ActionController::MCPServer
Exposes the application's annotated routes as an MCP server using the Streamable HTTP transport.
Controllers are presented as toolboxes and their routes as tools. To keep the model's context lean only these tools are listed, plus any root tools:
list_toolboxeslists the controllers and their descriptionsopen_toolbox(name)returns the definitions of the controller's toolscall_read_only(name, arguments)andcall_tool(name, arguments)run the tools
The server is stateless, there are no sessions, so requests can be served by any
instance of the application. Both the 2026-07-28 protocol and the earlier versions,
which start with initialize, are supported.
Controllers can also be served as their own MCP server (@[AC::MCP(endpoint: true)]),
and tools can render MCP Apps cards (@[AC::MCP(ui:)]), see ActionController::MCP.
Tool calls are dispatched in-process through the application router, so filters, authentication and error handlers apply exactly as they would for a regular request.
require "action-controller/mcp"
server = ActionController::Server.new
ActionController::MCPServer.mount(server, "/mcp")
server.run
Tool descriptions are extracted from the source code comments, so they need to be generated at build time and shipped alongside the binary:
ActionController::MCPServer.write_description("mcp.yml")
the file is lazily loaded from description_path the first time it is needed.
Routes can be excluded using @[AC::MCP(hide: true)]
Constants
protocol versions that start with initialize, newest first
stateless protocol versions, where every request carries its version in _meta
supported protocol versions, newest first
Class methods
browser origins permitted to connect, in addition to same origin requests.
"*" permits any origin
browser origins permitted to connect, in addition to same origin requests.
"*" permits any origin
how long a successful authentication check is cached for
a route used to authenticate MCP requests, i.e. /api/v1/users/current.
the route is requested in-process with the forwarded headers, a successful (2xx) response indicates the request is authenticated
a route used to authenticate MCP requests, i.e. /api/v1/users/current.
the route is requested in-process with the forwarded headers, a successful (2xx) response indicates the request is authenticated
authenticates MCP requests, returning true if the request is permitted.
optional, see auth_probe for a simpler alternative
authenticates MCP requests, returning true if the request is permitted.
optional, see auth_probe for a simpler alternative
how long clients can cache the server description and lists (ttlMs), 2026-07-28 onwards
how long clients can cache the server description and lists (ttlMs), 2026-07-28 onwards
location of the MCP description file, generated using write_description
response headers left out of tool results, matched case-insensitively. An entry
ending in * matches by prefix. Everything else (Link, X-Total-Count,
Content-Range, Location, ETag, ...) is returned to the model
response headers left out of tool results, matched case-insensitively. An entry
ending in * matches by prefix. Everything else (Link, X-Total-Count,
Content-Range, Location, ETag, ...) is returned to the model
request headers copied from the MCP request to the route being invoked, typically used for authentication
request headers copied from the MCP request to the route being invoked, typically used for authentication
usage instructions provided to the model, nil uses toolbox_instructions and an
empty string provides none. Include toolbox_instructions when describing your domain
how often an SSE comment is sent on idle event streams, which keeps proxies from timing them out and detects clients that have disconnected
how often an SSE comment is sent on idle event streams, which keeps proxies from timing them out and detects clients that have disconnected
how long a subscriptions/listen stream stays open, nil keeps it open until the
client disconnects. The stream is then completed and clients listen again
how long a subscriptions/listen stream stays open, nil keeps it open until the
client disconnects. The stream is then completed and clients listen again
the maximum number of resources a single subscriptions/listen can subscribe to
the maximum number of resources a single subscriptions/listen can subscribe to
advertises the OAuth authorization server so MCP clients can obtain and refresh access tokens. The request is provided for multi-tenant deployments
advertises the OAuth authorization server so MCP clients can obtain and refresh access tokens. The request is provided for multi-tenant deployments
serves an MCP Server Card
at <path>/server-card for each server mounted, so clients and registries can discover
how to connect before connecting. Cards name the server server_name in reverse-DNS
form, a name without a namespace is given the request host's (com.example.mcp/my-app)
serves an MCP Server Card
at <path>/server-card for each server mounted, so clients and registries can discover
how to connect before connecting. Cards name the server server_name in reverse-DNS
form, a name without a namespace is given the request host's (com.example.mcp/my-app)
how long clients and caches can keep a Server Card
what the server does, for its Server Card. Up to 100 characters, longer descriptions
are truncated. Defaults to server_title or server_name
what the server does, for its Server Card. Up to 100 characters, longer descriptions
are truncated. Defaults to server_title or server_name
the server's display name, reported to clients and in its Server Card
groups the tools into toolboxes, run by the call_read_only and call_tool meta
tools. When disabled every tool is listed
groups the tools into toolboxes, run by the call_read_only and call_tool meta
tools. When disabled every tool is listed
the folder of MCP Apps cards, @[AC::MCP(ui: "bookings/card.html")] renders
<ui_base>/bookings/card.html. Cards aren't served when nil
the folder of MCP Apps cards, @[AC::MCP(ui: "bookings/card.html")] renders
<ui_base>/bookings/card.html. Cards aren't served when nil
the default _meta.ui of every card (CSP domains, border...), a card can override it
with a .meta.json file next to it, i.e. bookings/card.meta.json
the default _meta.ui of every card (CSP domains, border...), a card can override it
with a .meta.json file next to it, i.e. bookings/card.meta.json
updates to a resource are sent at most once per period, clients read the resource for its current state so bursts of changes are coalesced
updates to a resource are sent at most once per period, clients read the resource for its current state so bursts of changes are coalesced
the server's homepage or documentation, reported to clients and in its Server Card
the server's homepage or documentation, reported to clients and in its Server Card
Instance methods
authentication is optional and enabled when any of authenticator,
auth_probe or resource_metadata is configured
the tool descriptions, lazily loaded from description_path.
if the file doesn't exist the description is generated from the compiled routes, however it will not include the documentation comments
replaces the current description, nil will reload it on next use
the path templates of the controller endpoints, @[AC::MCP(endpoint: true)]
generates the MCP description from the compiled routes.
docs: true extracts the source code comments using crystal docs,
which requires access to the source code
adds an icon for the global server, src follows the @[AC::Icon] rules and every
other argument is passed through as is
ActionController::MCPServer.icon "icons/logo.svg", sizes: ["any"]
ActionController::MCPServer.icon "https://example.com/logo-dark.png", sizes: ["48x48"], theme: "dark"
mounts the MCP endpoint at the path provided.
tool calls are dispatched via the router, typically ActionController::Server.
The OAuth protected resource metadata is served at /.well-known/oauth-protected-resource<path>
endpoints: true also mounts the controller endpoints, @[AC::MCP(endpoint: true)],
see mount_endpoints
mounts a browser test harness for the MCP Apps cards, intended for development.
Mount the MCP server first, the previewer connects to the path it was mounted at.
mcp overrides it, i.e. when a proxy serves the application under a prefix
GET <path> lists the cards and the tools that render them, each tool has a page
(<path>/tools/<name>) with a form for its arguments that renders the card in a
mock MCP Apps host. The page is an MCP client of the global MCP server, so tool calls,
including those made by the card, use the same routes and authentication as any
other client.
The tool pages accept query params, for linking and automated tests:
- the tool arguments by name,
?id=42, the card renders once all the required arguments are provided viewport_size=desktop|tablet|mobileandtheme=light|darkheader.<Name>=<value>request headers, i.e.header.Authorization=Bearer%20...
The page's assets are only compiled in when this method is called, see https://spider-gazelle.net/mcp/ui/ for excluding it from release builds.
mounts a server for each controller annotated @[AC::MCP(endpoint: true)]
the path of the global MCP server mounted on the router, nil if there isn't one
watches the resources of a template for changes, so clients can subscribe to them
using subscriptions/listen. The template must match a route annotated
@[AC::MCP(resource: template, subscribe: true)].
A client can only subscribe to resources it can read: the route is requested before the watcher is called. The block is called once per subscription and returns a proc that stops watching, called when the client disconnects
ActionController::MCPServer.watch("app://widgets/{id}") do |variables, _request, changed|
subscription = Widget.on_change(variables["id"]) { changed.call }
-> { subscription.cancel }
end
generates the description, including source code comments, and saves it to a file
Nested types
- ActionController::MCPServer::AuthCache
- ActionController::MCPServer::Description
- ActionController::MCPServer::Endpoint
- ActionController::MCPServer::Invoker
- ActionController::MCPServer::Prompt
- ActionController::MCPServer::PromptArgument
- ActionController::MCPServer::Protocol
- ActionController::MCPServer::RPCError
- ActionController::MCPServer::RequestContext
- ActionController::MCPServer::ResourceMetadata
- ActionController::MCPServer::StreamedResult
- ActionController::MCPServer::Tool
- ActionController::MCPServer::ToolParam
- ActionController::MCPServer::Toolbox
- ActionController::MCPServer::Transport
- ActionController::MCPServer::UICSP
- ActionController::MCPServer::UIMeta
- ActionController::MCPServer::URITemplate
- ActionController::MCPServer::Unauthorized
- ActionController::MCPServer::Watcher