meshagent-anthropic 0.23.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ from .messages_adapter import (
2
+ AnthropicMessagesAdapter,
3
+ AnthropicMessagesToolResponseAdapter,
4
+ )
5
+ from .mcp import (
6
+ MCPConfig,
7
+ MCPServer,
8
+ MCPTool,
9
+ MCPToolConfig,
10
+ MCPToolset,
11
+ MCPToolkitBuilder,
12
+ )
13
+ from .openai_responses_stream_adapter import AnthropicOpenAIResponsesStreamAdapter
14
+
15
+ __all__ = [
16
+ AnthropicMessagesAdapter,
17
+ AnthropicMessagesToolResponseAdapter,
18
+ AnthropicOpenAIResponsesStreamAdapter,
19
+ MCPConfig,
20
+ MCPServer,
21
+ MCPTool,
22
+ MCPToolConfig,
23
+ MCPToolset,
24
+ MCPToolkitBuilder,
25
+ ]
@@ -0,0 +1,103 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Literal, Optional
4
+
5
+ from pydantic import BaseModel
6
+
7
+ from meshagent.tools import BaseTool, Toolkit, ToolkitBuilder, ToolkitConfig
8
+
9
+
10
+ # This module wraps Anthropic's official MCP connector support:
11
+ # https://platform.claude.com/docs/en/agents-and-tools/mcp-connector
12
+
13
+
14
+ MCP_CONNECTOR_BETA = "mcp-client-2025-11-20"
15
+
16
+
17
+ class MCPServer(BaseModel):
18
+ """Anthropic `mcp_servers` entry."""
19
+
20
+ type: Literal["url"] = "url"
21
+ url: str
22
+ name: str
23
+ authorization_token: Optional[str] = None
24
+
25
+
26
+ class MCPToolConfig(BaseModel):
27
+ enabled: Optional[bool] = None
28
+ defer_loading: Optional[bool] = None
29
+
30
+
31
+ class MCPToolset(BaseModel):
32
+ """Anthropic `tools` entry for MCP connector."""
33
+
34
+ type: Literal["mcp_toolset"] = "mcp_toolset"
35
+ mcp_server_name: str
36
+ default_config: Optional[MCPToolConfig] = None
37
+ configs: Optional[dict[str, MCPToolConfig]] = None
38
+
39
+ # Pass-through cache control, if desired.
40
+ cache_control: Optional[dict] = None
41
+
42
+
43
+ class MCPConfig(ToolkitConfig):
44
+ """MeshAgent toolkit config that injects MCP connector params.
45
+
46
+ This is intentionally modeled after the OpenAI adapter's MCP config pattern
47
+ (a toolkit config that can be provided via `tools=[...]` in chat messages),
48
+ but it produces Anthropic-specific request parameters: `mcp_servers` and
49
+ `mcp_toolset` entries.
50
+ """
51
+
52
+ name: Literal["mcp"] = "mcp"
53
+
54
+ mcp_servers: list[MCPServer]
55
+ toolsets: Optional[list[MCPToolset]] = None
56
+ betas: list[str] = [MCP_CONNECTOR_BETA]
57
+
58
+
59
+ class MCPTool(BaseTool):
60
+ """Non-executable tool that augments the Anthropic request."""
61
+
62
+ def __init__(self, *, config: MCPConfig):
63
+ super().__init__(name="mcp")
64
+ self.config = config
65
+
66
+ def apply(self, *, request: dict) -> None:
67
+ """Mutate an Anthropic Messages request in-place."""
68
+
69
+ # Ensure we use the beta Messages API surface.
70
+ betas = request.setdefault("betas", [])
71
+ for b in self.config.betas:
72
+ if b not in betas:
73
+ betas.append(b)
74
+
75
+ toolsets = self.config.toolsets
76
+ if toolsets is None:
77
+ toolsets = [
78
+ MCPToolset(mcp_server_name=s.name) for s in self.config.mcp_servers
79
+ ]
80
+
81
+ # Merge/dedupe servers by name.
82
+ existing_servers = request.setdefault("mcp_servers", [])
83
+ dedup: dict[str, dict] = {
84
+ s["name"]: s
85
+ for s in existing_servers
86
+ if isinstance(s, dict) and isinstance(s.get("name"), str)
87
+ }
88
+ for server in self.config.mcp_servers:
89
+ dedup[server.name] = server.model_dump(mode="json", exclude_none=True)
90
+ request["mcp_servers"] = list(dedup.values())
91
+
92
+ # Anthropic MCP toolsets live inside the top-level `tools` array.
93
+ tools = request.setdefault("tools", [])
94
+ for toolset in toolsets:
95
+ tools.append(toolset.model_dump(mode="json", exclude_none=True))
96
+
97
+
98
+ class MCPToolkitBuilder(ToolkitBuilder):
99
+ def __init__(self):
100
+ super().__init__(name="mcp", type=MCPConfig)
101
+
102
+ async def make(self, *, room, model: str, config: MCPConfig) -> Toolkit:
103
+ return Toolkit(name="mcp", tools=[MCPTool(config=config)])