modelcontextprotocol/typescript-sdk

Error compiling schema.

Closed this issue · 3 comments

Describe the bug
When using typescript-sdk on restricted javascript execution environments we get the following error:
Image

This is because restricted javascript environments, like cloudflare workers or deno deploy do not accept eval, or new Function constructions. Since ajv.compile requires these dinamic evaluations, this error is logged to the console.

To Reproduce
Steps to reproduce the behavior:

  1. Run the server on restricted JS envs like cloudflare workers (wrangler to test it locally)
  2. Add tools and try to connect to the server. You'll see this error popup

Expected behavior
No error log to be logged to the console if these primitives are not available.

Additional context
Places that should be, somehow, guarded

const validate = ajv.compile(params.requestedSchema);

const validator = this._ajv.compile(tool.outputSchema);

this is making our error logs go crazy 🔥

Made a small patch. Just use this class while this issue is open

class CustomClient extends Client {
  constructor(_clientInfo: Implementation, options?: ClientOptions) {
    super(_clientInfo, options);
  }

  override async listTools(
    params?: ListToolsRequest["params"],
    options?: RequestOptions,
  ) {
    const result = await this.request(
      { method: "tools/list", params },
      ListToolsResultSchema,
      options,
    );

    // Cache the tools and their output schemas for future validation
    // this.cacheToolOutputSchemas(result.tools);

    return result;
  }
}

Related ajv issues: #689, potentially resolved by #1012 which replaces ajv