Skip to content

MCP instrumentation retries synchronously failing handlers #24504

Description

@betegon

Wrapping an MCP server with wrapMcpServerWithSentry can execute a synchronous handler twice when its first invocation throws. This repeats application side effects. If the second invocation succeeds, the MCP client receives that success instead of the original failure.

Reproduced on develop at ceb8517523 with the official TypeScript SDK 1.30.0 and 2.0.0 clients and servers, including in a deployed Cloudflare Worker. Tools, resources, and prompts registered after wrapping are affected; resource callbacks registered before wrapping are also affected. Unwrapped callbacks and asynchronous rejections execute once.

For example, register this tool after wrapping, then call it once through an MCP client:

let calls = 0;
const server = wrapMcpServerWithSentry(new McpServer({ name: 'example', version: '1.0.0' }));

server.registerTool('example', {}, () => {
  calls += 1;
  if (calls === 1) {
    throw new Error('operation failed');
  }
  return { content: [{ type: 'text', text: 'unexpected retry' }] };
});

Expected: calls === 1 and the tool error reaches the client. Actual: calls === 2 and the client receives unexpected retry as a successful result.

The inner handler wrapper captures and rethrows the application error. The outer fallback catches that same error and invokes the original callback again, treating it as an instrumentation failure.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCorejavascriptPull requests that update javascript code

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions