MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / callMCPTool

Function callMCPTool

common/src/mcp/client.ts:148–197  ·  view source on GitHub ↗
(
  clientId: string,
  ...args: Parameters<typeof Client.prototype.callTool>
)

Source from the content-addressed store, hash-verified

146}
147
148export async function callMCPTool(
149 clientId: string,
150 ...args: Parameters<typeof Client.prototype.callTool>
151): Promise<ToolResultOutput[]> {
152 const client = runningClients[clientId]
153 if (!client) {
154 throw new Error(`callTool: client not found with id: ${clientId}`)
155 }
156 const callResult = await client.callTool(...args)
157 const result = callResult as CallToolResult
158 const content = result.content
159
160 return content.map((c: (typeof content)[number]) => {
161 if (c.type === 'text') {
162 return {
163 type: 'json',
164 value: c.text,
165 } satisfies ToolResultOutput
166 }
167 if (c.type === 'audio') {
168 return {
169 type: 'media',
170 data: c.data,
171 mediaType: c.mimeType,
172 } satisfies ToolResultOutput
173 }
174 if (c.type === 'image') {
175 return {
176 type: 'media',
177 data: c.data,
178 mediaType: c.mimeType,
179 } satisfies ToolResultOutput
180 }
181 if (c.type === 'resource') {
182 return {
183 type: 'media',
184 data: getResourceData(c.resource),
185 mediaType: c.resource.mimeType ?? 'text/plain',
186 } satisfies ToolResultOutput
187 }
188 const fallbackValue =
189 'uri' in c && typeof (c as { uri: unknown }).uri === 'string'
190 ? (c as { uri: string }).uri
191 : JSON.stringify(c)
192 return {
193 type: 'json',
194 value: fallbackValue,
195 } satisfies ToolResultOutput
196 })
197}

Callers 1

handleToolCallFunction · 0.90

Calls 1

getResourceDataFunction · 0.85

Tested by

no test coverage detected