|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/** |
| 3 | + * Connect to the MCP server using SSE transport and call a tool. |
| 4 | + * The Actors MCP Server will load default Actors. |
| 5 | + * |
| 6 | + * !!! This example needs to be fixed as it does not work !!! |
| 7 | + */ |
| 8 | + |
| 9 | +import path from 'path'; |
| 10 | +import { fileURLToPath } from 'url'; |
| 11 | + |
| 12 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 13 | +import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'; |
| 14 | +import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js'; |
| 15 | +import dotenv from 'dotenv'; |
| 16 | +import { EventSource } from 'eventsource'; |
| 17 | + |
| 18 | +// Resolve dirname equivalent in ES module |
| 19 | +const filename = fileURLToPath(import.meta.url); |
| 20 | +const dirname = path.dirname(filename); |
| 21 | + |
| 22 | +dotenv.config({ path: path.resolve(dirname, '../../.env') }); |
| 23 | + |
| 24 | +const SERVER_URL = 'https://actors-mcp-server/sse'; |
| 25 | +// We need to change forward slash / to underscore _ in the tool name as Anthropic does not allow forward slashes in the tool name |
| 26 | +const SELECTED_TOOL = 'apify_rag-web-browser'; |
| 27 | + |
| 28 | +if (!process.env.APIFY_TOKEN) { |
| 29 | + console.error('APIFY_TOKEN is required but not set in the environment variables.'); |
| 30 | + process.exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +if (typeof globalThis.EventSource === 'undefined') { |
| 34 | + globalThis.EventSource = EventSource as unknown as typeof globalThis.EventSource; |
| 35 | +} |
| 36 | + |
| 37 | +async function main(): Promise<void> { |
| 38 | + const transport = new SSEClientTransport( |
| 39 | + new URL(SERVER_URL), |
| 40 | + { |
| 41 | + requestInit: { |
| 42 | + headers: { |
| 43 | + authorization: `Bearer ${process.env.APIFY_TOKEN}`, |
| 44 | + }, |
| 45 | + }, |
| 46 | + eventSourceInit: { |
| 47 | + // The EventSource package augments EventSourceInit with a "fetch" parameter. |
| 48 | + // You can use this to set additional headers on the outgoing request. |
| 49 | + // Based on this example: https://github.com/modelcontextprotocol/typescript-sdk/issues/118 |
| 50 | + async fetch(input: Request | URL | string, init?: RequestInit) { |
| 51 | + const headers = new Headers(init?.headers || {}); |
| 52 | + headers.set('authorization', `Bearer ${process.env.APIFY_TOKEN}`); |
| 53 | + return fetch(input, { ...init, headers }); |
| 54 | + }, |
| 55 | + // We have to cast to "any" to use it, since it's non-standard |
| 56 | + } as any, // eslint-disable-line @typescript-eslint/no-explicit-any |
| 57 | + }, |
| 58 | + ); |
| 59 | + const client = new Client( |
| 60 | + { name: 'example-client', version: '1.0.0' }, |
| 61 | + { capabilities: {} }, |
| 62 | + ); |
| 63 | + |
| 64 | + try { |
| 65 | + // Connect to the MCP server |
| 66 | + await client.connect(transport); |
| 67 | + |
| 68 | + // List available tools |
| 69 | + const tools = await client.listTools(); |
| 70 | + console.log('Available tools:', tools); |
| 71 | + |
| 72 | + if (tools.tools.length === 0) { |
| 73 | + console.log('No tools available'); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const selectedTool = tools.tools.find((tool) => tool.name === SELECTED_TOOL); |
| 78 | + if (!selectedTool) { |
| 79 | + console.error(`The specified tool: ${selectedTool} is not available. Exiting.`); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Call a tool |
| 84 | + console.log('Calling actor ...'); |
| 85 | + const result = await client.callTool( |
| 86 | + { name: SELECTED_TOOL, arguments: { query: 'web browser for Anthropic' } }, |
| 87 | + CallToolResultSchema, |
| 88 | + ); |
| 89 | + console.log('Tool result:', JSON.stringify(result, null, 2)); |
| 90 | + } catch (error: unknown) { |
| 91 | + if (error instanceof Error) { |
| 92 | + console.error('Error:', error.message); |
| 93 | + console.error(error.stack); |
| 94 | + } else { |
| 95 | + console.error('An unknown error occurred:', error); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +await main(); |
0 commit comments