Skip to content

Commit 92613e9

Browse files
chore(mcp): add friendlier MCP code tool errors on incorrect method invocations
1 parent c65e4a3 commit 92613e9

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

packages/mcp-server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@valtown/deno-http-worker": "^0.0.21",
3737
"cors": "^2.8.5",
3838
"express": "^5.1.0",
39+
"fuse.js": "^7.1.0",
3940
"jq-web": "https://github.com/stainless-api/jq-web/releases/download/v0.8.6/jq-web.tar.gz",
4041
"qs": "^6.14.0",
4142
"typescript": "5.8.3",

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import util from 'node:util';
44

5+
import Fuse from 'fuse.js';
56
import ts from 'typescript';
67

78
import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types';
@@ -39,8 +40,98 @@ function getRunFunctionNode(
3940
return null;
4041
}
4142

43+
const fuse = new Fuse(
44+
[
45+
'client.casParser.camsKfintech',
46+
'client.casParser.cdsl',
47+
'client.casParser.nsdl',
48+
'client.casParser.smartParse',
49+
'client.casGenerator.generateCas',
50+
],
51+
{ threshold: 1, shouldSort: true },
52+
);
53+
54+
function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
55+
return fuse
56+
.search(fullyQualifiedMethodName)
57+
.map(({ item }) => item)
58+
.slice(0, 5);
59+
}
60+
61+
const proxyToObj = new WeakMap<any, any>();
62+
const objToProxy = new WeakMap<any, any>();
63+
64+
type ClientProxyConfig = {
65+
path: string[];
66+
isBelievedBad?: boolean;
67+
};
68+
69+
function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
70+
let proxy: T = objToProxy.get(obj);
71+
72+
if (!proxy) {
73+
proxy = new Proxy(obj, {
74+
get(target, prop, receiver) {
75+
const propPath = [...path, String(prop)];
76+
const value = Reflect.get(target, prop, receiver);
77+
78+
if (isBelievedBad || (!(prop in target) && value === undefined)) {
79+
// If we're accessing a path that doesn't exist, it will probably eventually error.
80+
// Let's proxy it and mark it bad so that we can control the error message.
81+
// We proxy an empty class so that an invocation or construction attempt is possible.
82+
return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
83+
}
84+
85+
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
86+
return makeSdkProxy(value, { path: propPath, isBelievedBad });
87+
}
88+
89+
return value;
90+
},
91+
92+
apply(target, thisArg, args) {
93+
if (isBelievedBad || typeof target !== 'function') {
94+
const fullyQualifiedMethodName = path.join('.');
95+
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
96+
throw new Error(
97+
`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
98+
);
99+
}
100+
101+
return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
102+
},
103+
104+
construct(target, args, newTarget) {
105+
if (isBelievedBad || typeof target !== 'function') {
106+
const fullyQualifiedMethodName = path.join('.');
107+
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
108+
throw new Error(
109+
`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
110+
);
111+
}
112+
113+
return Reflect.construct(target, args, newTarget);
114+
},
115+
});
116+
117+
objToProxy.set(obj, proxy);
118+
proxyToObj.set(proxy, obj);
119+
}
120+
121+
return proxy;
122+
}
123+
42124
const fetch = async (req: Request): Promise<Response> => {
43125
const { opts, code } = (await req.json()) as WorkerInput;
126+
if (code == null) {
127+
return Response.json(
128+
{
129+
message:
130+
'The code param is missing. Provide one containing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
131+
} satisfies WorkerError,
132+
{ status: 400, statusText: 'Code execution error' },
133+
);
134+
}
44135

45136
const runFunctionNode = getRunFunctionNode(code);
46137
if (!runFunctionNode) {
@@ -73,7 +164,7 @@ const fetch = async (req: Request): Promise<Response> => {
73164
${code}
74165
run_ = run;
75166
`);
76-
const result = await run_(client);
167+
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
77168
return Response.json({
78169
result,
79170
logLines,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function codeTool(): Promise<Endpoint> {
2323
const tool: Tool = {
2424
name: 'execute',
2525
description:
26-
'Runs TypeScript code to interact with the API.\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client, and it will be run.\nDo not initialize a client, but instead use the client that you are given as a parameter.\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
26+
'Runs TypeScript code to interact with the API.\n\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client named "client", and it will be run.\nWrite code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```\n\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
2727
inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
2828
};
2929

0 commit comments

Comments
 (0)