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/agents- User data products for customer AI agents/api/mcp/observatory- System catalog (mz_*) for troubleshooting
Β§Tools
Agents: get_data_products, get_data_product_details, query
Observatory: query_system_catalog
Data products are discovered via mz_internal.mz_mcp_data_products system view.
ModulesΒ§
- error_
codes π - JSON-RPC 2.0 error codes.
StructsΒ§
- Capabilities π
- Client
Info π - Content
Block π - GetData
Product πDetails Params - Initialize
Params π - Initialize
Result π - McpError π
- McpRequest π
- JSON-RPC 2.0 request. Requests have
id; notifications donβt. - McpResponse π
- Query
Params π - Query
System πCatalog Params - Read
Data πProduct Params - 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.
- Tools
Call πParams - Tool call parameters, deserialized via adjacently tagged enum.
Serde maps
nameto the variant andargumentsto the variantβs data.
ConstantsΒ§
- DETAILS_
QUERY_ πPREFIX - DISCOVERY_
QUERY π - JSONRPC_
VERSION π - JSON-RPC protocol version used in all MCP requests and responses.
- MAX_
READ_ πLIMIT - Maximum number of rows that can be returned by read_data_product.
- MCP_
PROTOCOL_ πVERSION - MCP protocol version returned in the
initializeresponse. Spec: https://modelcontextprotocol.io/specification/2025-11-25 - MCP_
REQUEST_ πTIMEOUT - Maximum time an MCP tool call can run before the HTTP response is returned. Note: this returns a clean JSON-RPC error to the caller, but the underlying query may continue running on the cluster until it completes or is cancelled separately (see database-issues#9947 for SELECT timeout gaps).
- READ_
ONLY_ πANNOTATIONS - Annotations for all MCP tools: read-only, non-destructive, idempotent.
FunctionsΒ§
- default_
read_ πlimit - 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_
initialize π - handle_
mcp_ agents - Agents endpoint: exposes user data products.
- 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_ observatory - Observatory endpoint: exposes system catalog (mz_*) only.
- handle_
mcp_ πrequest - handle_
mcp_ πrequest_ inner - handle_
tools_ πcall - handle_
tools_ πlist - query_
system_ πcatalog - read_
data_ πproduct - Read rows from a data product. Issues a single read-only query.
- safe_
data_ πproduct_ name - Parses a data product name and returns it safely quoted for SQL interpolation.
- validate_
origin π - Validates the Origin header to prevent DNS rebinding attacks (MCP 2025-11-25). Returns Some(403) if the Origin is present but doesnβt match the Host. Returns None if the Origin is absent (non-browser client) or valid.
- 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.