GenMCP.Transport.StreamableHTTP (gen_mcp v0.9.0)

Copy Markdown View Source

Handles incoming MCP requests over HTTP with SSE support.

This module is a Plug that can be mounted in your router. It handles the MCP protocol handshake, session management, and request routing.

It supports Server-Sent Events (SSE) for streaming responses, such as notifications and asynchronous tool results.

Configuration

Options for the HTTP connection

  • :assigns (map/0) - A map of assigns to define to the channel passed to tools, resources, etc. The default value is %{}.

  • :copy_assigns (list of atom/0) - A list of assigns keys that will be copied from the conn to the channel. Those will overwrite the assigns from the :assigns option above. The default value is [].

Options for the MCP OTP session wrapper

Note that the :session_controller is managed directly by the GenMCP behaviour implementation.

  • :server - The GenMCP behaviour server implemetation that will handle MCP messages. If a simple atom, it will receive all other options given to the session. The default value is GenMCP.Suite.

  • :session_timeout (integer/0) - Session will automatically terminate when not receiving any request or notification for that number of milliseconds. The default value is 120000.

Options for the GenMCP behaviour implmentation

All other options given to GenMCP.Transport.StreamableHTTP will be forwarded to the server implementation.

The default server, GenMCP.Suite, will accept the following options:

  • :server_name (String.t/0) - Required.

  • :server_version (String.t/0) - Required.

  • :server_title (String.t/0)

  • :tools - The list of GenMCP.Suite.Tool implementations that will be available in the server. List items can be either module names, {module, arg} tuples or a descriptor map. The default value is [].

  • :resources - The list of GenMCP.Suite.ResourceRepo implementations to serve resources from. List items can be either module names, {module, arg} tuples or a descriptor map. The default value is [].

  • :prompts - A list of GenMCP.Suite.PromptRepo implementations to generate prompts with. List items can be either module names, {module, arg} tuples or a descriptor map. The default value is [].

  • :extensions - A list GenMCP.Suite.Extension implementations to add more tools, resource repositories and prompt repositories. List items can be either module names, {module, arg} tuples or a descriptor map. The default value is [].

Example

# In your router
forward "/mcp", GenMCP.Transport.StreamableHTTP,
  server_name: "My App",
  server_version: "1.0.0",
  tools: [MyTool]

Summary

Functions

Callback implementation for Plug.call/2.

Defines a module that delegates to GenMCP.Transport.StreamableHTTP.

Callback implementation for Plug.init/1.

Functions

call(conn, opts)

Callback implementation for Plug.call/2.

defplug(module)

(macro)

Defines a module that delegates to GenMCP.Transport.StreamableHTTP.

This is useful if you want to define a named Plug for your MCP server.

Example

defmodule MyMCPPlug do
  require GenMCP.Transport.StreamableHTTP
  GenMCP.Transport.StreamableHTTP.defplug(MyMCPPlug)
end

init(opts)

Callback implementation for Plug.init/1.