Skip to content

Server crashes on unhandled MCP methods (ping, unknown methods) #1

@dzuluaga

Description

@dzuluaga

The MCP C++ server crashes (segfault) when receiving standard MCP methods like ping or any unknown method. This is a critical issue for production use and MCP conformance.

Steps to Reproduce

  1. Start the MCP server
  2. Initialize a session
  3. Send a ping request:
{"jsonrpc": "2.0", "id": 99, "method": "ping"}
  1. Server crashes immediately

Root Cause

In src/shared/base_session.cpp, line ~102:

void BaseSession::ProcessIncomingRequest(const JSONRPCRequest& rpcRequest, RequestContext& ctx)
{
    if (!rpcRequest.request_) {
        MCP_LOG(MCP_LOG_LEVEL_ERROR, "rpcRequest.request_ is null");
    }  // <-- BUG: no return here
    const Request& typedRequest = *static_cast<const Request*>(rpcRequest.request_.get());
    // ^^ null pointer dereference → segfault

When a method like ping arrives, the JSON parser can't deserialize it into any known request type (no PingRequest class exists), so rpcRequest.request_ is nullptr. The code logs the error but doesn't return — it falls through and dereferences the null pointer.

Proposed Fix

Fix 1 — Add return after null check (base_session.cpp):

if (!rpcRequest.request_) {
    MCP_LOG(MCP_LOG_LEVEL_ERROR, "rpcRequest.request_ is null");
    return;  // ADD THIS
}

Fix 2 — Add ping handler (mcp_server_implement.cpp, in ReceiveIncomingMessages):

if (method == "ping") {
    auto session = serverManager_->GetSession(ctx.sessionId);
    if (session) {
        auto result = std::make_unique<Result>();
        session->SendResponse(requestId, std::move(result), ctx);
    }
    return;
}

Impact

  • Any MCP client sending ping (including the official MCP Inspector) will crash the server
  • Per the MCP spec, ping is a standard method that all servers should handle
  • Per JSON-RPC 2.0, unknown methods should return error code -32601, never crash

Environment

  • MCP C++ SDK from openJiuwen-ai/agent-protocol
  • Tested via MCP Inspector v0.21.1 and curl
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions