Skip to content

bug(mcp): MCP tools crash server if provider throws — no error handling in get_current_context / format_context_for_prompt #272

Closed

Description

@vamgan

Summary

The get_current_context and format_context_for_prompt MCP tool implementations in packages/mcp/src/index.ts call options.provider.getContext(args) with no try/catch. If the provider throws or returns an invalid packet, the MCP server crashes without sending a well-formed error response to the calling agent.

Root cause

// packages/mcp/src/index.ts ~lines 125-136
handler: async (args) => {
  const packet = await options.provider.getContext(args); // no try/catch
  return { content: [{ type: 'text', text: JSON.stringify(packet) }] };
}

Impact

  • A buggy or malicious provider can crash the entire MCP server process, disconnecting Claude/agents mid-session
  • The calling agent receives no structured error — just a hanging or dropped connection

Fix

Wrap provider calls in try/catch and return a structured MCP error response:

handler: async (args) => {
  try {
    const packet = await options.provider.getContext(args);
    return { content: [{ type: 'text', text: JSON.stringify(packet) }] };
  } catch (err) {
    return {
      isError: true,
      content: [{ type: 'text', text: `Failed to get context: ${err instanceof Error ? err.message : String(err)}` }],
    };
  }
}

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions