Skip to content

[STDIO] BadMapError when processing incoming messages #240

@b-serra

Description

@b-serra

Description

The STDIO transport crashes with a BadMapError when processing any incoming JSON-RPC message.

Steps to Reproduce

  1. Start an MCP server with stdio transport
  2. Send any valid JSON-RPC message via stdin (e.g., an initialize request)
  3. Server crashes with BadMapError

Example:

echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | mix mcp.server

Expected Behavior

The server should process the message and return a valid JSON-RPC response.

Actual Behavior

** (BadMapError) expected a map, got:
    [
      %{
        "id" => 1,
        "jsonrpc" => "2.0",
        "method" => "initialize",
        ...
      }
    ]

Root Cause

In lib/hermes/server/transport/stdio.ex, the handle_incoming_data/2 function calls Message.decode/1 which returns {:ok, messages} where messages is a list of decoded messages. However, this list is passed directly to process_message/2 which expects a single message map.

# Current code (line 241-243):
case Message.decode(data) do
  {:ok, messages} ->
    process_message(messages, state)  # Bug: messages is a list, not a map

Proposed Fix

Iterate over the messages list before calling process_message/2:

case Message.decode(data) do
  {:ok, messages} ->
    Enum.each(messages, &process_message(&1, state))

Environment

  • hermes_mcp version: 0.14.1
  • Elixir version: 1.15+
  • OS: macOS/Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions