Skip to content

Commit 8c380a8

Browse files
fix(mcp): avoid importing unsupported libraries on non-node environments
1 parent 39cdb76 commit 8c380a8

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

packages/mcp-server/src/code-tool.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { Endpoint, ContentBlock, Metadata } from './tools/types';
77

88
import { Tool } from '@modelcontextprotocol/sdk/types.js';
99

10-
import { newDenoHTTPWorker } from '@valtown/deno-http-worker';
1110
import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
12-
import { workerPath } from './code-tool-paths.cjs';
1311

1412
/**
1513
* A tool that runs code against a copy of the SDK.
@@ -20,7 +18,7 @@ import { workerPath } from './code-tool-paths.cjs';
2018
*
2119
* @param endpoints - The endpoints to include in the list.
2220
*/
23-
export function codeTool(): Endpoint {
21+
export async function codeTool(): Promise<Endpoint> {
2422
const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
2523
const tool: Tool = {
2624
name: 'execute',
@@ -29,6 +27,10 @@ export function codeTool(): Endpoint {
2927
inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
3028
};
3129

30+
// Import dynamically to avoid failing at import time in cases where the environment is not well-supported.
31+
const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
32+
const { workerPath } = await import('./code-tool-paths.cjs');
33+
3234
const handler = async (client: Finch, args: unknown) => {
3335
const baseURLHostname = new URL(client.baseURL).hostname;
3436
const { code } = args as { code: string };

packages/mcp-server/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function main() {
1414
return;
1515
}
1616

17-
const selectedTools = selectToolsOrError(endpoints, options);
17+
const selectedTools = await selectToolsOrError(endpoints, options);
1818

1919
console.error(
2020
`MCP Server starting with ${selectedTools.length} tools:`,
@@ -47,9 +47,9 @@ function parseOptionsOrError() {
4747
}
4848
}
4949

50-
function selectToolsOrError(endpoints: Endpoint[], options: McpOptions): Endpoint[] {
50+
async function selectToolsOrError(endpoints: Endpoint[], options: McpOptions): Promise<Endpoint[]> {
5151
try {
52-
const includedTools = selectTools(endpoints, options);
52+
const includedTools = await selectTools(endpoints, options);
5353
if (includedTools.length === 0) {
5454
console.error('No tools match the provided filters.');
5555
process.exit(1);

packages/mcp-server/src/server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function initMcpServer(params: {
5555
let providedEndpoints: Endpoint[] | null = null;
5656
let endpointMap: Record<string, Endpoint> | null = null;
5757

58-
const initTools = (implementation?: Implementation) => {
58+
const initTools = async (implementation?: Implementation) => {
5959
if (implementation && (!mcpOptions.client || mcpOptions.client === 'infer')) {
6060
mcpOptions.client =
6161
implementation.name.toLowerCase().includes('claude') ? 'claude'
@@ -66,8 +66,8 @@ export function initMcpServer(params: {
6666
...mcpOptions.capabilities,
6767
};
6868
}
69-
providedEndpoints = selectTools(endpoints, mcpOptions);
70-
endpointMap = Object.fromEntries(providedEndpoints.map((endpoint) => [endpoint.tool.name, endpoint]));
69+
providedEndpoints ??= await selectTools(endpoints, mcpOptions);
70+
endpointMap ??= Object.fromEntries(providedEndpoints.map((endpoint) => [endpoint.tool.name, endpoint]));
7171
};
7272

7373
const client = new Finch({
@@ -82,7 +82,7 @@ export function initMcpServer(params: {
8282

8383
server.setRequestHandler(ListToolsRequestSchema, async () => {
8484
if (providedEndpoints === null) {
85-
initTools(server.getClientVersion());
85+
await initTools(server.getClientVersion());
8686
}
8787
return {
8888
tools: providedEndpoints!.map((endpoint) => endpoint.tool),
@@ -91,7 +91,7 @@ export function initMcpServer(params: {
9191

9292
server.setRequestHandler(CallToolRequestSchema, async (request) => {
9393
if (endpointMap === null) {
94-
initTools(server.getClientVersion());
94+
await initTools(server.getClientVersion());
9595
}
9696
const { name, arguments: args } = request.params;
9797
const endpoint = endpointMap![name];
@@ -106,7 +106,7 @@ export function initMcpServer(params: {
106106
/**
107107
* Selects the tools to include in the MCP Server based on the provided options.
108108
*/
109-
export function selectTools(endpoints: Endpoint[], options?: McpOptions): Endpoint[] {
109+
export async function selectTools(endpoints: Endpoint[], options?: McpOptions): Promise<Endpoint[]> {
110110
const filteredEndpoints = query(options?.filters ?? [], endpoints);
111111

112112
let includedTools = filteredEndpoints;
@@ -121,7 +121,7 @@ export function selectTools(endpoints: Endpoint[], options?: McpOptions): Endpoi
121121
} else if (options?.includeDynamicTools) {
122122
includedTools = dynamicTools(endpoints);
123123
} else if (options?.includeCodeTools) {
124-
includedTools = [codeTool()];
124+
includedTools = [await codeTool()];
125125
} else {
126126
includedTools = endpoints;
127127
}

0 commit comments

Comments
 (0)