MCP instrumentation retries synchronously failing handlers

Author: betegonCreated Sep 18, 2026Updated Sep 18, 2026
LabelsBugjavascriptCore

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:

typescript
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.

Source: getsentry/sentry-javascript