McpHouse.Server (mcp_house v0.1.0)

Copy Markdown View Source

A Plug serving MCP tools over HTTP. By default it accepts only 2026-07-28. Enable 2025-11-25, 2025-06-18, or 2025-03-26 for Streamable HTTP handshakes. Enable 2024-11-05 for HTTP+SSE. List the desired revisions in the mount's :protocol_versions option. Set :sessions to bind handshake sessions to authenticated principals through a McpHouse.Server.SessionStore implementation.

Forward the MCP path to this module with the tool options described in init/1. The host supplies the HTTP listener and authenticates requests before forwarding them. Handlers receive the connection and can use its authentication assigns for authorization.

Invoke a tool with Plug.Test

iex> echo = %{
...>   name: "echo",
...>   description: "Return the supplied text",
...>   input_schema: %{
...>     "type" => "object",
...>     "properties" => %{"text" => %{"type" => "string"}},
...>     "required" => ["text"]
...>   },
...>   handler: fn %{"text" => text}, _conn -> {:ok, text} end
...> }
iex> opts = McpHouse.Server.init(name: "Example", version: "1.0", tools: [echo])
iex> body = Jason.encode!(%{
...>   jsonrpc: "2.0", id: 1, method: "tools/call",
...>   params: %{name: "echo", arguments: %{text: "hello"}, _meta: %{
...>     "io.modelcontextprotocol/protocolVersion" => "2026-07-28",
...>     "io.modelcontextprotocol/clientCapabilities" => %{}
...>   }}
...> })
iex> conn = (
...>   Plug.Test.conn(:post, "/mcp", body)
...>   |> Plug.Conn.put_req_header("content-type", "application/json")
...>   |> Plug.Conn.put_req_header("accept", "application/json")
...>   |> Plug.Conn.put_req_header("mcp-protocol-version", "2026-07-28")
...>   |> Plug.Conn.put_req_header("mcp-method", "tools/call")
...>   |> Plug.Conn.put_req_header("mcp-name", "echo")
...>   |> McpHouse.Server.call(opts)
...> )
iex> {conn.status, get_in(Jason.decode!(conn.resp_body), ["result", "content"])}
{200, [%{"type" => "text", "text" => "hello"}]}

Summary

Functions

Handles an MCP request and returns the connection with its response sent.

Compiles the tool registry and input schemas from the router options.

Milliseconds left in the current handler execution, or :infinity.

Functions

call(conn, opts)

Handles an MCP request and returns the connection with its response sent.

Modern JSON-RPC requests require an application/json body and an Accept header allowing JSON. The Mcp-Protocol-Version and Mcp-Method headers must match the request. Tool calls also carry Mcp-Name and any parameter headers required by the tool schema. When the Plug reads the body directly it enforces a 1 MiB limit. With Plug.Parsers upstream, configure the body limit on that parser.

iex> opts = McpHouse.Server.init(name: "Example", version: "1", tools: [])
iex> conn = Plug.Test.conn(:get, "/mcp") |> McpHouse.Server.call(opts)
iex> {conn.status, Plug.Conn.get_resp_header(conn, "allow")}
{405, ["POST"]}

init(opts)

Compiles the tool registry and input schemas from the router options.

:protocol_versions is an ordered list of revision strings. For stateful handshakes, :sessions accepts a :store tuple {module, ref}, binary :namespace, a :principal function of one connection, and optional :idle_timeout and :max_lifetime durations in milliseconds. Use a stable nonempty binary principal. :handler_timeout defaults to :infinity; a positive millisecond value bounds dispatch, with a per-tool override.

Supply :name, :version, and an ordered :tools list. Each tool is a map with :name, :description, a string-keyed JSON :input_schema with type "object", and a :handler function of two arguments. Tool names must be unique. Pass the returned configuration to call/2.

The handler receives the validated arguments and the Plug.Conn. Return {:ok, value} for success or {:error, value} for a tool error. A UTF-8 string becomes text content. A JSON object appears in both "structuredContent" and the JSON-encoded text content. An optional :annotations map is included in the tool listing. A schema property annotated with "x-mcp-header" requires the matching MCP parameter header when the caller provides that argument.

Set :tool_visible? to a function of tool name and connection that returns true for tools the caller may list or invoke. Set :allowed_origins to the exact browser origins that may call the endpoint. Requests without an Origin header are accepted.

iex> tool = %{
...>   name: "echo", description: "Echo", input_schema: %{"type" => "object"},
...>   handler: fn args, _conn -> {:ok, args} end
...> }
iex> McpHouse.Server.init(name: "Example", version: "1", tools: [tool, tool])
** (ArgumentError) duplicate MCP tool name

remaining_time(conn)

Milliseconds left in the current handler execution, or :infinity.