MCP Library¶
The MCP (Match Condition Parameters) CEL extension enables parsing and extracting arguments from an MCP JSON payload, as specified in Envoy's Match Condition Parameters.
This library allows Kyverno policies to extract, filter, and use MCP/proxy metadata in CEL expressions.
Types¶
<MCP>¶
CEL Type / Proto: mcp.MCP
This is an opaque type that represents an MCP parser instance.
<MCPRequest>¶
CEL Type / Proto: mcp.MCPRequest
Represents a parsed MCP request, providing accessors for extracting arguments.
The MCPRequest struct contains the parsed MCP request data. It includes:
Method(string): The MCP method nameID(RequestId): The request identifierPaginated(*PaginatedParams): Pagination parameters for all list methods
Tool-related fields: - ToolCall (*CallToolParams): Tool call parameters
Resource-related fields: - ResourceRead (ReadResourceParams): Resource read parameters - *ResourceSubscribe** (SubscribeParams): Resource subscription parameters - *ResourceUnsubscribe** (*UnsubscribeParams): Resource unsubscription parameters
Prompt-related fields: - PromptGet (*GetPromptParams): Prompt retrieval parameters
Lifecycle and utility fields: - Initialize (InitializeParams): Lifecycle initialization parameters - *CreateMessage** (CreateMessageParams): Message creation parameters - *Elicitation** (ElicitationParams): Elicitation parameters - *Complete** (CompleteParams): Completion utility parameters - *SetLogLevel** (*SetLevelParams): Log level setting parameters
These fields are conditionally populated based on the request method and type that was received (eg: ToolCall will only be populated if MCP method was tools/call).
Constants¶
The following values representing known MCP methods are available as constants for use in expressions (exposed as mcp.<name>):
mcp.InitializeMethodmcp.PingMethodmcp.ResourcesListMethodmcp.ResourcesTemplatesListMethodmcp.ResourcesReadMethodmcp.PromptsListMethodmcp.PromptsGetMethodmcp.ToolsListMethodmcp.ToolsCallMethodmcp.SetLogLevelMethodmcp.ElicitationCreateMethod
Functions¶
Parse¶
Parses a raw MCP JSON string using a given MCP parser instance.
Signature¶
Example¶
// "rawMcpJson" holds the MCP JSON string (usually the proxy's metadata)
mcpRequest := mcpInstance.Parse(rawMcpJson)
GetStringArgument¶
Returns the string value of an argument given its key, or a default if missing.
Signature¶
Example¶
GetIntArgument¶
Returns the integer value of an argument given its key, or a default if missing.
Signature¶
Example¶
GetFloatArgument¶
Returns the float (double) value of an argument given its key, or a default if missing.
Signature¶
Example¶
GetBoolArgument¶
Returns the boolean value of an argument given its key, or a default if missing.
Signature¶
Example¶
GetStringSliceArgument¶
Returns a list of strings for an argument given its key, or a default if missing.
Signature¶
Example¶
GetIntSliceArgument¶
Returns a list of integers for an argument given its key, or a default if missing.
Signature¶
Example¶
GetFloatSliceArgument¶
Returns a list of floats for an argument given its key, or a default if missing.
Signature¶
GetBoolSliceArgument¶
Returns a list of booleans for an argument given its key, or a default if missing.
Signature¶
Example: Using MCP with Kyverno CEL¶
Let's say we want to check a tool's usage:
// Suppose you have received an MCP JSON string in input.mcp
mcpReq = mcpInstance.Parse(input.mcp)
// Check the MCP request's method & tool usage name
isAllowed = mcpReq.Method == mcp.ToolsCallMethod && mcpReq.ToolCall.Name == "shell"
// Check for a particular tool argument
command = mcpReq.GetStringArgument("command", "")
isAuthorized = isCallMethod && isShellTool && command in ["kubectl", "docker", ""]