googleapis/mcp-toolbox · error
INVALID_REQUEST
INVALID_REQUEST
Error message
invalid mcp initialize request: %w
What it means
The initialize handler unmarshals the request body into InitializeRequest. If the JSON is malformed or does not match the expected shape (missing/invalid protocolVersion, capabilities, or clientInfo fields), the server rejects it with JSON-RPC INVALID_REQUEST and the message 'invalid mcp initialize request'. InitializeRequest embeds ProtocolVersion and ClientInfo which must be present per the MCP spec.
Source
Thrown at internal/server/mcp/v20250618/method.go:73
return promptsGetHandler(ctx, id, g, primitiveMgr, body)
default:
err := fmt.Errorf("invalid method %s", method)
return jsonrpc.NewError(id, jsonrpc.METHOD_NOT_FOUND, err.Error(), nil), err
}
}
// InitializeResponse runs capability negotiation and protocol version agreement.
// This is the Initialization phase of the lifecycle for MCP client-server connections.
// Always start with the latest protocol version supported.
func initializeHandler(ctx context.Context, id jsonrpc.RequestId, body []byte) (any, error) {
v, err := util.ToolboxVersionFromContext(ctx)
if err != nil {
return jsonrpc.NewError(id, jsonrpc.INTERNAL_ERROR, err.Error(), nil), err
}
var req InitializeRequest
if err := json.Unmarshal(body, &req); err != nil {
err = fmt.Errorf("invalid mcp initialize request: %w", err)
return jsonrpc.NewError(id, jsonrpc.INVALID_REQUEST, err.Error(), nil), err
}
toolsListChanged := false
promptsListChanged := false
result := InitializeResult{
ProtocolVersion: PROTOCOL_VERSION,
Capabilities: ServerCapabilities{
Tools: &ListChanged{
ListChanged: &toolsListChanged,
},
Prompts: &ListChanged{
ListChanged: &promptsListChanged,
},
},
ServerInfo: Implementation{
BaseMetadata: BaseMetadata{
Name: SERVER_NAME,View on GitHub (pinned to 8cc6e09de2)
Solutions
- Send a valid JSON body shaped like {"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"my-client","version":"1.0.0"}}
- Validate the body with a JSON linter / JSON schema before calling initialize
- Use an official MCP client SDK instead of hand-building the initialize payload
- Capture the underlying %w error text in the response — it names the exact unmarshal failure (field and type)
Example fix
// before
{"protocolVersion":"2025-06-18","clientInfo":{"name":"x","version":"1"}}
// after
{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"x","version":"1"}} Defensive patterns
Strategy: validation
Validate before calling
const body = { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "my-client", version: "1.0.0" } };
if (typeof body.protocolVersion !== "string" || !body.clientInfo?.name) throw new Error("invalid initialize payload"); Type guard
function isInitializeRequest(b: unknown): b is { protocolVersion: string; capabilities: object; clientInfo: { name: string; version: string } } {
const r = b as any;
return !!r && typeof r.protocolVersion === "string" && typeof r.clientInfo === "object" && r.clientInfo !== null && typeof r.capabilities === "object";
} Try / catch
try { const res = await initialize(body); } catch (e) { if (e.code === -32600) { console.error("initialize payload rejected:", e.message); } else throw e; } Prevention
- Validate payloads against the MCP initialize JSON schema before sending
- Always include protocolVersion, capabilities, and clientInfo
- Test handshake with a known-good client first
When it happens
Trigger: POSTing a body to the initialize endpoint that fails json.Unmarshal against InitializeRequest: non-JSON body, wrong field types (e.g. protocolVersion as number instead of string), or missing required nested objects.
Common situations: Hand-rolled clients omitting clientInfo or capabilities; sending the MCP session body as form data instead of JSON; version skew where a client sends an initialize shape from an older spec; JSON with trailing commas or single quotes.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/d2b7b91a7ac7a591.
Report an issue: GitHub.