Expand description
Model Context Protocol (MCP) HTTP handlers.
Exposes Materialize data products to AI agents via JSON-RPC 2.0 over HTTP POST.
Β§Endpoints
/api/mcp/agent- User data products for customer AI agents/api/mcp/developer- System catalog (mz_*) for troubleshooting
Β§Tools
Agent: get_data_products, get_data_product_details, read_data_product, query
Developer: query_system_catalog, query
read_data_product and both query tools are dyncfg-gated.
Data products are discovered via mz_internal.mz_mcp_data_products system view.
ModulesΒ§
- error_
codes π - JSON-RPC 2.0 error codes.
StructsΒ§
- Cache
Hints π - Required on cacheable results, such as
tools/list. - Capabilities π
- Client
Info π - Common
Result πFields - Content
Block π - Discover
Result π - GetData
Product πDetails Params - Initialize
Params π - Initialize
Result π - McpError π
- McpHeaders π
- The request headers the Streamable HTTP transport defines, with every occurrence kept: a repeated header is rejected rather than reduced to one.
- McpRequest π
- JSON-RPC 2.0 request. Requests have
id; notifications donβt. - McpResponse π
- McpResult
Body π - A result as sent.
commonisNoneon 2025-11-25, whose results carry neither field. - NoArguments π
- Arguments of a tool that takes none. A struct rather than
(): read from aserde_json::Value,()accepts{}only if serde buffersargumentsahead ofname, which depends on key order (preserve_orderkeeps the clientβs order). - Query
Params π - Query
System πCatalog Params - RawRequest π
- The body read as untyped JSON, for the 2026-07-28 checks on fields the typed parse drops. Only 2026-07-28 requests pay for the second read.
- Read
Data πProduct Params - Result
Meta π - Server
Info π - Table
Reference πCollector - Collects table references from SQL AST with their schema qualification.
- Tool
Annotations π - MCP 2025-11-25 tool annotations that describe tool behavior. These hints help clients make trust and safety decisions.
- Tool
Content πResult - Tool
Definition π - Tools
List πResult
EnumsΒ§
- McpEndpoint
Type π - McpMethod π
- MCP method variants with their associated parameters.
- McpRequest
Error π - MCP request errors, mapped to JSON-RPC error codes.
- McpResult π
- Typed MCP response results.
- Protocol
Version π - The protocol revision a request is served on.
- Tool
Name π - The tool name a requestβs
Mcp-Nameheader must mirror. - Tools
Call πParams - Tool call parameters, deserialized via adjacently tagged enum.
Serde maps
nameto the variant andargumentsto the variantβs data.
ConstantsΒ§
- CACHE_
TTL_ πMS - How long a client may consider a cacheable result fresh, in milliseconds. Kept short so flag changes reach clients soon.
- DEFAULT_
READ_ πLIMIT - Default row cap for
read_data_productwhen the caller omitslimit. - DETAILS_
QUERY_ πPREFIX - DISCOVERY_
QUERY π - JSONRPC_
VERSION π - JSON-RPC protocol version used in all MCP requests and responses.
- MCP_
PROTOCOL_ πVERSION - MCP protocol version returned in the
initializeresponse. Spec: https://modelcontextprotocol.io/specification/2025-11-25 - READ_
ONLY_ πANNOTATIONS - Annotations for all MCP tools: read-only, non-destructive, idempotent.
FunctionsΒ§
- answer_
parse_ πfailure - Answers a 2026-07-28 request whose body did not parse, in JSON-RPC. One that
passes
check_modern_requestgets 404 for a method 2026-07-28 does not define, and-32602otherwise. Other rejections keep axumβs response. - as_
unknown_ πmethod - The request
bodywould produce ifMcpMethod::Unknownacceptedparams, orNonewhen its method is one we know, whose parse error must stand. - asks_
for_ π2026_ 07_ 28 - Whether any
MCP-Protocol-Versioncopy names 2026-07-28, also inside a comma-joined list. Whether it is served on it also depends on the flag (seeProtocolVersion::select). - build_
read_ πquery - Builds the SQL the agent runs for
read_data_product. - call_
status π - Maps a handler result to its metric
McpCallStatus. Errors carry the closederror_type()label; anything else isOk. - check_
modern_ πrequest - Runs the checks every 2026-07-28 request gets before dispatch: a JSON-RPC
2.0 envelope, the mirrored headers, and
params._meta. - check_
request_ πmeta - Checks the
params._metafields every 2026-07-28 request must carry: the protocol version, matching its header, and the client capabilities. - decode_
mcp_ πname - Decodes the
=?base64?...?=form clients use for names that are not plain ASCII. Any other value is the name as is. - default_
read_ πlimit - deserialize_
tools_ πcall argumentsis optional in the MCP spec, but adjacent tagging requires the content key, so a call that omits it, or sendsnull, is given{}.- endpoint_
instructions π - Instructions returned by
initializeandserver/discoverfor each endpoint type. These guide the AI agent on how to use the server correctly. - execute_
query π - execute_
sql π - Execute SQL via
execute_requestfrom sql.rs. - format_
rows_ πresponse - Serialize rows to JSON and enforce the response size cap.
- get_
data_ πproduct_ details - get_
data_ πproducts - handle_
discover π - handle_
initialize π - handle_
mcp_ agent - Agent endpoint: exposes user data products.
- handle_
mcp_ developer - Developer endpoint: exposes system catalog (mz_*) only.
- handle_
mcp_ πmethod - handle_
mcp_ method_ not_ allowed - MCP 2025-11-25 requires servers to return 405 for GET requests on endpoints that only support POST.
- handle_
mcp_ πrequest - handle_
mcp_ πrequest_ inner - handle_
tools_ πcall - handle_
tools_ πlist - header_
text π - invalid_
id π - is_
json_ πcontent_ type - The
Content-Typecheck axumβsJsonextractor runs before parsing. - is_
valid_ πrequest_ id - MCP request ids are strings or numbers. Any other id,
nullincluded, gets-32600with no id in the reply. - parse_
mcp_ πrequest - Parses a request body the way axumβs
Jsonextractor does, so every rejection keeps its status and text. The one exception: an unknown method that carriesparamsbecomesMcpMethod::Unknowninstead of a 422 (seeas_unknown_method). The body arrives throughBytes, so the routeβsDefaultBodyLimitstill applies. - present_
id π - Wraps a
nullid inSome, so it stays a request. A missingidisNonethrough#[serde(default)]on the field, and only that is a notification. - query_
set_ πclause - Builds the
SETclause forexecute_query: alwaysSET CLUSTER, plusSET CLUSTER_REPLICAwhen a replica is requested (e.g. forEXPLAIN ANALYZEon a cluster with multiple replicas). Both names pass throughescaped_string_literalsince they are interpolated into SQL string literals. - query_
system_ πcatalog - read_
data_ πproduct - Read rows from a data product. Issues a single read-only query.
- read_
only_ πtxn - Wraps
bodyin aBEGIN READ ONLY; <set_clause>; <body>; COMMIT;frame soset_clauseis scoped to this read and does not leak into the session. - safe_
data_ πproduct_ name - Parses a data product name and returns it safely quoted for SQL interpolation.
- select_
single_ πrows - Returns the rows of the single row-returning statement in a response.
- server_
info π - validate_
cluster_ πreplica - Rejects an empty or whitespace-only
cluster_replica. Such a name would otherwise produceSET CLUSTER_REPLICA = '', which fails deep in the engine as a generic execution error rather than a clean validation error.None(no replica pin requested) is always valid. - validate_
origin π - Validates the Origin header against the CORS allowlist to prevent DNS rebinding attacks (MCP spec 2025-11-25). Returns Some(403) if Origin is present but not on the allowlist. Returns None if absent (non-browser client) or allowed.
- validate_
readonly_ πquery - Validates query is a single SELECT, SHOW, or EXPLAIN statement.
- validate_
system_ πcatalog_ query - Validates that a query only references system catalog tables.