MCPcopy Create free account
hub / github.com/Noumena-Network/code / processMcpResourceAttachments

Function processMcpResourceAttachments

src/utils/attachments.ts:2001–2067  ·  view source on GitHub ↗
(
  input: string,
  toolUseContext: ToolUseContext,
)

Source from the content-addressed store, hash-verified

1999}
2000
2001async function processMcpResourceAttachments(
2002 input: string,
2003 toolUseContext: ToolUseContext,
2004): Promise<Attachment[]> {
2005 const resourceMentions = extractMcpResourceMentions(input)
2006 if (resourceMentions.length === 0) return []
2007
2008 const mcpClients = toolUseContext.options.mcpClients || []
2009
2010 const results = await Promise.all(
2011 resourceMentions.map(async mention => {
2012 try {
2013 const [serverName, ...uriParts] = mention.split(':')
2014 const uri = uriParts.join(':') // Rejoin in case URI contains colons
2015
2016 if (!serverName || !uri) {
2017 logEvent('ncode_at_mention_mcp_resource_error', {})
2018 return null
2019 }
2020
2021 // Find the MCP client
2022 const client = mcpClients.find(c => c.name === serverName)
2023 if (!client || client.type !== 'connected') {
2024 logEvent('ncode_at_mention_mcp_resource_error', {})
2025 return null
2026 }
2027
2028 // Find the resource in available resources to get its metadata
2029 const serverResources =
2030 toolUseContext.options.mcpResources?.[serverName] || []
2031 const resourceInfo = serverResources.find(r => r.uri === uri)
2032 if (!resourceInfo) {
2033 logEvent('ncode_at_mention_mcp_resource_error', {})
2034 return null
2035 }
2036
2037 try {
2038 const result = await client.client.readResource({
2039 uri,
2040 })
2041
2042 logEvent('ncode_at_mention_mcp_resource_success', {})
2043
2044 return {
2045 type: 'mcp_resource' as const,
2046 server: serverName,
2047 uri,
2048 name: resourceInfo.name || uri,
2049 description: resourceInfo.description,
2050 content: result,
2051 }
2052 } catch (error) {
2053 logEvent('ncode_at_mention_mcp_resource_error', {})
2054 logError(error)
2055 return null
2056 }
2057 } catch {
2058 logEvent('ncode_at_mention_mcp_resource_error', {})

Callers 1

getAttachmentsFunction · 0.85

Calls 3

logErrorFunction · 0.70
logEventFunction · 0.50

Tested by

no test coverage detected