AshAi.Mcp.Server (ash_ai v0.6.1)

Copy Markdown View Source

Implementation of the Model Context Protocol (MCP) RPC functionality.

This module handles HTTP requests and responses according to the MCP specification, supporting both synchronous and streaming communication patterns. It also handles the core JSON-RPC message processing for the protocol.

Summary

Functions

Get the MCP server name

Get the MCP server version

Handle HTTP DELETE request for session termination

Process an HTTP GET request to open an SSE stream

Process an HTTP POST request containing JSON-RPC messages

Create a standard JSON-RPC error response

Parse the JSON-RPC request

Process a single JSON-RPC message

Computes the sandbox domain for an mcp_ui_resource from the MCP server URL.

Send an SSE event over the chunked connection

Functions

get_server_name(opts)

Get the MCP server name

get_server_version(opts)

Get the MCP server version

handle_delete(conn, session_id)

Handle HTTP DELETE request for session termination

handle_get(conn, session_id)

Process an HTTP GET request to open an SSE stream

handle_post(conn, body, session_id, opts \\ [])

Process an HTTP POST request containing JSON-RPC messages

json_rpc_error_response(id, code, message, data \\ nil)

Create a standard JSON-RPC error response

parse_json_rpc(request)

Parse the JSON-RPC request

process_message(message, session_id, opts)

Process a single JSON-RPC message

sandbox_domain(server_url)

Computes the sandbox domain for an mcp_ui_resource from the MCP server URL.

MCP hosts render UI resources in sandboxed iframes, and each host determines the iframe's origin differently:

HostDomain formatBehavior
Claude{sha256_hash}.claudemcpcontent.comHash derived from the MCP server endpoint URL.
ChatGPT{connector_id}.web-sandbox.oaiusercontent.comAuto-assigned by ChatGPT; ignores the domain field.

Since ChatGPT ignores domain entirely, this function generates a Claude-compatible value so that a single configuration works across both hosts.

When domain is set to :auto (the default), this is called automatically at request time using the server URL derived from the incoming connection.

Examples

iex> AshAi.Mcp.Server.sandbox_domain("http://localhost:4000/mcp")
"0307c5dc3988887979d60ecbb5101189.claudemcpcontent.com"

send_sse_event(conn, event, data, id \\ nil)

Send an SSE event over the chunked connection