( clientId: string, ...args: Parameters<typeof Client.prototype.callTool> )
| 146 | } |
| 147 | |
| 148 | export 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 | } |
no test coverage detected