@apify/actors-mcp-server
Version:
Model Context Protocol Server for Apify
69 lines • 2.6 kB
JavaScript
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import log from '@apify/log';
import { getMCPServerID } from './utils.js';
/**
* Creates and connects a ModelContextProtocol client.
* First tries streamable HTTP transport, then falls back to SSE transport.
*/
export async function connectMCPClient(url, token) {
try {
return await createMCPStreamableClient(url, token);
}
catch {
// If streamable HTTP transport fails, fall back to SSE transport
log.debug('Streamable HTTP transport failed, falling back to SSE transport', {
url,
});
return await createMCPSSEClient(url, token);
}
}
/**
* Creates and connects a ModelContextProtocol client.
*/
async function createMCPSSEClient(url, token) {
const transport = new SSEClientTransport(new URL(url), {
requestInit: {
headers: {
authorization: `Bearer ${token}`,
},
},
eventSourceInit: {
// The EventSource package augments EventSourceInit with a "fetch" parameter.
// You can use this to set additional headers on the outgoing request.
// Based on this example: https://github.com/modelcontextprotocol/typescript-sdk/issues/118
async fetch(input, init) {
const headers = new Headers((init === null || init === void 0 ? void 0 : init.headers) || {});
headers.set('authorization', `Bearer ${token}`);
return fetch(input, { ...init, headers });
},
// We have to cast to "any" to use it, since it's non-standard
}, // eslint-disable-line @typescript-eslint/no-explicit-any
});
const client = new Client({
name: getMCPServerID(url),
version: '1.0.0',
});
await client.connect(transport);
return client;
}
/**
* Creates and connects a ModelContextProtocol client using the streamable HTTP transport.
*/
async function createMCPStreamableClient(url, token) {
const transport = new StreamableHTTPClientTransport(new URL(url), {
requestInit: {
headers: {
authorization: `Bearer ${token}`,
},
},
});
const client = new Client({
name: getMCPServerID(url),
version: '1.0.0',
});
await client.connect(transport);
return client;
}
//# sourceMappingURL=client.js.map