airflow.providers.common.ai.hooks.mcp¶
Classes¶
Hook for connecting to MCP (Model Context Protocol) servers. |
Module Contents¶
- class airflow.providers.common.ai.hooks.mcp.MCPHook(mcp_conn_id=default_conn_name, tool_prefix=None, *, token_provider=None, **kwargs)[source]¶
Bases:
airflow.providers.common.compat.sdk.BaseHookHook for connecting to MCP (Model Context Protocol) servers.
Manages connection configuration for MCP servers. Supports three transport types: HTTP (Streamable HTTP), SSE, and stdio.
- Connection fields:
host: Server URL for HTTP/SSE transports (e.g.
http://localhost:3001/mcp)password: Auth token (optional)
Extra.transport: Transport type —
http(default),sse, orstdioExtra.command: Command to run for stdio transport (e.g.
uvx)Extra.args: Command arguments for stdio transport (e.g.
["mcp-run-python"])Extra.timeout: Connection timeout in seconds for stdio (default: 10)
For HTTP/SSE transports the
Authorizationheader is, by default, a staticBearertoken taken from the connectionpassword. Endpoints that require a freshly minted or short-lived token (e.g. a Snowflake managed MCP server authenticated with a key-pair JWT, OAuth/refresh tokens, Workload Identity Federation, or GitHub App installation tokens) can pass atoken_providercallable instead. It is invoked each time the server connection is established and its return value is used as the bearer token, so a fresh token is minted without storing a long-lived secret in the connection.- Parameters:
mcp_conn_id (str) – Airflow connection ID for the MCP server.
tool_prefix (str | None) – Optional prefix prepended to tool names (e.g.
"weather"→"weather_get_forecast").token_provider (collections.abc.Callable[[], str] | None) – Optional zero-argument callable returning a bearer token string. When set, it overrides the connection
passwordfor theAuthorizationheader on HTTP/SSE transports and is called each time the server connection is established. Ignored for thestdiotransport.
- static get_ui_field_behaviour()[source]¶
Return custom field behaviour for the Airflow connection form.
- get_conn()[source]¶
Return a configured PydanticAI MCP server instance.
Creates the appropriate MCP server based on the transport type in the connection’s extra field:
http(default):MCPServerStreamableHTTPsse:MCPServerSSEstdio:MCPServerStdio
The result is cached for the lifetime of this hook instance.