MCP API¶
mcp ¶
MCP integration for dynamic tool discovery and server exposure.
Provides MCPServerManager for connecting to MCP servers and using their tools as pydantic-ai tools, MCPToolAdapter for bridging MCP→pydantic-ai, and create_orqest_server for exposing Orqest as an MCP server.
Auto-discovery is gated by :class:PermissionGate (default
:class:DenyAll); two integration paths are available:
- :meth:
ToolRegistry.get_or_discover— deliberate lookup (called by code that knows it needs a tool now). - :class:
DiscoveryHook— opportunistic recovery from runtime "tool not found" errors raised by hallucinating LLMs.
MCPToolAdapter ¶
Convert MCP tool definitions into pydantic-ai Tool instances.
MCP tools are described by name, description, and a JSON Schema for
their parameters. This adapter creates an async wrapper function that
calls the MCP server and packages the result as a pydantic-ai Tool.
adapt
staticmethod
¶
Create a pydantic-ai Tool from a single MCP tool definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The MCP tool name. |
required |
tool_description
|
str
|
Human-readable description shown to the LLM. |
required |
input_schema
|
dict[str, Any]
|
JSON Schema for the tool's input parameters. |
required |
call_fn
|
Any
|
Async callable |
required |
Returns:
| Type | Description |
|---|---|
Tool
|
A pydantic-ai |
Source code in orqest/mcp/adapter.py
adapt_many
staticmethod
¶
Convert a list of MCP tool definitions to pydantic-ai Tools.
Each entry in tool_defs must have name and may have
description and inputSchema keys (matching the MCP
ListToolsResult shape).
Source code in orqest/mcp/adapter.py
MCPConnection ¶
A live connection to a single MCP server.
Manages the transport, session, and adapted tool list for one server.
Initialize with server config. Call connect() to start.
Source code in orqest/mcp/client.py
connect
async
¶
Establish the MCP connection and discover tools.
Branches on config.transport: "stdio" launches the server
process via stdio_client; "sse" connects to config.url
via sse_client; "streamable-http" connects via
streamablehttp_client with config.headers (the canonical
transport for Tier-2 :class:DockerSandbox). The Streamable HTTP
transport yields a (read, write, _session_id_callback) triple;
the rest yield (read, write). We normalize on the first two.
Source code in orqest/mcp/client.py
disconnect
async
¶
Close the connection gracefully.
Source code in orqest/mcp/client.py
MCPServerManager ¶
Manage connections to multiple MCP servers.
Handles lifecycle, auto-discovery from standard config paths, and
provides a unified tool list across all connected servers. Supports
async with for automatic cleanup.
Initialize the manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MCPConfig | None
|
Explicit MCP configuration. When auto_discover is
|
None
|
Source code in orqest/mcp/client.py
connect_all
async
¶
Connect to all configured + discovered servers.
Source code in orqest/mcp/client.py
connect
async
¶
Connect to a single MCP server.
Raises:
| Type | Description |
|---|---|
Exception
|
Propagated from the transport layer on failure. |
Source code in orqest/mcp/client.py
disconnect_all
async
¶
disconnect
async
¶
discover_and_connect
async
¶
Search online for MCP servers matching a capability and connect.
Uses MCPDiscovery to find servers by keyword, then connects
to each. This is the key method that enables agents to
dynamically expand their toolset at runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Capability description (e.g., "SQL database", "GitHub"). |
required |
max_servers
|
int
|
Maximum number of servers to connect to. |
3
|
Returns:
| Type | Description |
|---|---|
list[MCPConnection]
|
List of newly established connections. |
Source code in orqest/mcp/client.py
get_all_tools ¶
Collect tools from all live connections.
get_tools ¶
search_tools ¶
Keyword search across all connected servers.
Source code in orqest/mcp/client.py
discover_local_configs
staticmethod
¶
Scan standard paths for MCP server configurations.
Checks ~/.claude.json, ~/.claude/claude.json, and
~/.config/Claude/claude_desktop_config.json.
Source code in orqest/mcp/client.py
MCPConfig
dataclass
¶
Top-level MCP integration configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
servers
|
list[MCPServerConfig]
|
Explicit server configurations. |
list()
|
auto_discover
|
bool
|
Scan standard paths for MCP configs on connect. |
True
|
MCPServerConfig
dataclass
¶
MCPServerConfig(
name: str,
command: str,
args: list[str] = list(),
env: dict[str, str] = dict(),
transport: str = "stdio",
url: str | None = None,
headers: dict[str, str] = dict(),
)
Configuration for a single MCP server connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable identifier for this server. |
required |
command
|
str
|
Executable to launch (e.g., "python", "node", "npx"). |
required |
args
|
list[str]
|
Command-line arguments (e.g., ["-m", "my_mcp_server"]). |
list()
|
env
|
dict[str, str]
|
Environment variables passed to the server process. |
dict()
|
transport
|
str
|
Connection transport — |
'stdio'
|
url
|
str | None
|
Server URL for |
None
|
headers
|
dict[str, str]
|
HTTP headers attached to every request. Used to carry
session/auth tokens (e.g.
|
dict()
|
DiscoveredServer
dataclass
¶
DiscoveredServer(
name: str,
description: str,
url: str,
tools: list[str] = list(),
source: str = "registry",
metadata: dict[str, Any] = dict(),
)
A server found via online discovery.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Server identifier. |
description |
str
|
What the server provides. |
url |
str
|
Connection endpoint (SSE or Streamable HTTP). |
tools |
list[str]
|
Tool names advertised by the server. |
source |
str
|
Where this was discovered ("registry", "wellknown", "search"). |
metadata |
dict[str, Any]
|
Additional discovery metadata. |
to_config ¶
Convert to an MCPServerConfig for connection.
MCPDiscovery ¶
MCPDiscovery(
*,
registry_urls: list[str] | None = None,
well_known_urls: list[str] | None = None,
timeout: float = 15.0,
)
Discover MCP servers online by querying registry endpoints.
Enables agents to find tools they need at runtime without any pre-configuration. The MetaOrchestrator uses this when no local tool matches a subtask's requirements. Preview — see the module docstring for current limitations.
Usage::
discovery = MCPDiscovery()
servers = await discovery.search("database SQL query")
for server in servers:
config = server.to_config()
await manager.connect(config)
Initialize discovery.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry_urls
|
list[str] | None
|
Custom registry search endpoints. Defaults to the official MCP registry and Glama. |
None
|
well_known_urls
|
list[str] | None
|
Base URLs to probe for |
None
|
timeout
|
float
|
HTTP request timeout in seconds. |
15.0
|
Source code in orqest/mcp/discovery.py
search
async
¶
Search for MCP servers matching a capability query.
Probes any configured well_known_urls first (explicitly
configured = highest intent), then queries the registry endpoints,
deduplicating by server name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Natural language description of needed capability (e.g., "SQL database queries", "GitHub repository access"). |
required |
max_results
|
int
|
Maximum servers to return. |
5
|
Returns:
| Type | Description |
|---|---|
list[DiscoveredServer]
|
Discovered servers, well-known manifests ahead of registry hits. |
Source code in orqest/mcp/discovery.py
probe_wellknown
async
¶
Probe a URL's /.well-known/mcp.json for server metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
The server's base URL (e.g., "https://mcp.example.com"). |
required |
Returns:
| Type | Description |
|---|---|
DiscoveredServer | None
|
A DiscoveredServer if the manifest exists, else None. |
Source code in orqest/mcp/discovery.py
DiscoveryHook ¶
DiscoveryHook(
registry: ToolRegistry,
discovery: MCPDiscovery,
manager: MCPServerManager,
*,
permission: PermissionGate | None = None,
audit_bus: EventBus | None = None,
)
ToolHook that recovers from "tool not found" via MCP discovery.
The hook's :meth:on_error returns :class:Redirect(new_tool=name)
after the tool is registered (caller should retry with the registered
tool), or :class:Continue otherwise — including when the gate denies
or discovery fails.
Source code in orqest/mcp/discovery_hook.py
AllowAll ¶
Permit any tool name. Use only for development / trusted environments.
AllowList ¶
Permit tool names matching any of the supplied regex patterns.
Patterns are compiled with :func:re.search semantics — they match
anywhere in the tool name. Anchor with ^…$ for full-name matches.
Source code in orqest/mcp/permission.py
DenyAll ¶
Deny every discovery. The default — discovery is opt-in.
PermissionGate ¶
Decide whether a tool name may be discovered + registered at runtime.