acp-sdk 0.0.6__py3-none-any.whl → 1.0.0rc1__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.
- acp_sdk/client/__init__.py +1 -0
- acp_sdk/client/client.py +135 -0
- acp_sdk/models.py +219 -0
- acp_sdk/server/__init__.py +2 -0
- acp_sdk/server/agent.py +32 -0
- acp_sdk/server/bundle.py +133 -0
- acp_sdk/server/context.py +6 -0
- acp_sdk/server/server.py +137 -0
- acp_sdk/server/telemetry.py +45 -0
- acp_sdk/server/utils.py +12 -0
- acp_sdk-1.0.0rc1.dist-info/METADATA +53 -0
- acp_sdk-1.0.0rc1.dist-info/RECORD +15 -0
- acp/__init__.py +0 -138
- acp/cli/__init__.py +0 -6
- acp/cli/claude.py +0 -139
- acp/cli/cli.py +0 -471
- acp/client/__main__.py +0 -79
- acp/client/session.py +0 -372
- acp/client/sse.py +0 -145
- acp/client/stdio.py +0 -153
- acp/server/__init__.py +0 -3
- acp/server/__main__.py +0 -50
- acp/server/highlevel/__init__.py +0 -9
- acp/server/highlevel/agents/__init__.py +0 -5
- acp/server/highlevel/agents/agent_manager.py +0 -110
- acp/server/highlevel/agents/base.py +0 -20
- acp/server/highlevel/agents/templates.py +0 -21
- acp/server/highlevel/context.py +0 -185
- acp/server/highlevel/exceptions.py +0 -25
- acp/server/highlevel/prompts/__init__.py +0 -4
- acp/server/highlevel/prompts/base.py +0 -167
- acp/server/highlevel/prompts/manager.py +0 -50
- acp/server/highlevel/prompts/prompt_manager.py +0 -33
- acp/server/highlevel/resources/__init__.py +0 -23
- acp/server/highlevel/resources/base.py +0 -48
- acp/server/highlevel/resources/resource_manager.py +0 -94
- acp/server/highlevel/resources/templates.py +0 -80
- acp/server/highlevel/resources/types.py +0 -185
- acp/server/highlevel/server.py +0 -705
- acp/server/highlevel/tools/__init__.py +0 -4
- acp/server/highlevel/tools/base.py +0 -83
- acp/server/highlevel/tools/tool_manager.py +0 -53
- acp/server/highlevel/utilities/__init__.py +0 -1
- acp/server/highlevel/utilities/func_metadata.py +0 -210
- acp/server/highlevel/utilities/logging.py +0 -43
- acp/server/highlevel/utilities/types.py +0 -54
- acp/server/lowlevel/__init__.py +0 -3
- acp/server/lowlevel/helper_types.py +0 -9
- acp/server/lowlevel/server.py +0 -643
- acp/server/models.py +0 -17
- acp/server/session.py +0 -315
- acp/server/sse.py +0 -175
- acp/server/stdio.py +0 -83
- acp/server/websocket.py +0 -61
- acp/shared/__init__.py +0 -0
- acp/shared/context.py +0 -14
- acp/shared/exceptions.py +0 -14
- acp/shared/memory.py +0 -87
- acp/shared/progress.py +0 -40
- acp/shared/session.py +0 -413
- acp/shared/version.py +0 -3
- acp/types.py +0 -1258
- acp_sdk-0.0.6.dist-info/METADATA +0 -46
- acp_sdk-0.0.6.dist-info/RECORD +0 -57
- acp_sdk-0.0.6.dist-info/entry_points.txt +0 -2
- acp_sdk-0.0.6.dist-info/licenses/LICENSE +0 -22
- {acp/client → acp_sdk}/__init__.py +0 -0
- {acp → acp_sdk}/py.typed +0 -0
- {acp_sdk-0.0.6.dist-info → acp_sdk-1.0.0rc1.dist-info}/WHEEL +0 -0
acp/server/highlevel/server.py
DELETED
@@ -1,705 +0,0 @@
|
|
1
|
-
"""FastMCP - A more ergonomic interface for MCP servers."""
|
2
|
-
|
3
|
-
import inspect
|
4
|
-
import json
|
5
|
-
import re
|
6
|
-
from itertools import chain
|
7
|
-
from typing import Any, Callable, Literal, Sequence, Type
|
8
|
-
|
9
|
-
import anyio
|
10
|
-
import pydantic_core
|
11
|
-
import uvicorn
|
12
|
-
from pydantic import BaseModel, Field
|
13
|
-
from pydantic.networks import AnyUrl
|
14
|
-
from pydantic_settings import BaseSettings, SettingsConfigDict
|
15
|
-
|
16
|
-
from acp.server.highlevel.agents import Agent, AgentManager, AgentTemplate
|
17
|
-
from acp.server.highlevel.context import Context
|
18
|
-
from acp.server.highlevel.exceptions import ResourceError
|
19
|
-
from acp.server.highlevel.prompts import Prompt, PromptManager
|
20
|
-
from acp.server.highlevel.resources import FunctionResource, Resource, ResourceManager
|
21
|
-
from acp.server.highlevel.tools import ToolManager
|
22
|
-
from acp.server.highlevel.utilities.logging import configure_logging, get_logger
|
23
|
-
from acp.server.highlevel.utilities.types import Image
|
24
|
-
from acp.server.lowlevel import Server as MCPServer
|
25
|
-
from acp.server.lowlevel.helper_types import ReadResourceContents
|
26
|
-
from acp.server.sse import SseServerTransport
|
27
|
-
from acp.server.stdio import stdio_server
|
28
|
-
from acp.types import (
|
29
|
-
Agent as MCPAgent,
|
30
|
-
)
|
31
|
-
from acp.types import (
|
32
|
-
AgentTemplate as MCPAgentTemplate,
|
33
|
-
)
|
34
|
-
from acp.types import (
|
35
|
-
AnyFunction,
|
36
|
-
CreateAgentRequest,
|
37
|
-
CreateAgentResult,
|
38
|
-
DestroyAgentRequest,
|
39
|
-
DestroyAgentResult,
|
40
|
-
EmbeddedResource,
|
41
|
-
GetPromptResult,
|
42
|
-
ImageContent,
|
43
|
-
ListAgentsRequest,
|
44
|
-
ListAgentsResult,
|
45
|
-
ListAgentTemplatesRequest,
|
46
|
-
ListAgentTemplatesResult,
|
47
|
-
RunAgentRequest,
|
48
|
-
RunAgentResult,
|
49
|
-
TextContent,
|
50
|
-
)
|
51
|
-
from acp.types import (
|
52
|
-
Prompt as MCPPrompt,
|
53
|
-
)
|
54
|
-
from acp.types import (
|
55
|
-
PromptArgument as MCPPromptArgument,
|
56
|
-
)
|
57
|
-
from acp.types import (
|
58
|
-
Resource as MCPResource,
|
59
|
-
)
|
60
|
-
from acp.types import (
|
61
|
-
ResourceTemplate as MCPResourceTemplate,
|
62
|
-
)
|
63
|
-
from acp.types import (
|
64
|
-
Tool as MCPTool,
|
65
|
-
)
|
66
|
-
|
67
|
-
logger = get_logger(__name__)
|
68
|
-
|
69
|
-
|
70
|
-
class Settings(BaseSettings):
|
71
|
-
"""FastMCP server settings.
|
72
|
-
|
73
|
-
All settings can be configured via environment variables with the prefix FASTMCP_.
|
74
|
-
For example, FASTMCP_DEBUG=true will set debug=True.
|
75
|
-
"""
|
76
|
-
|
77
|
-
model_config = SettingsConfigDict(
|
78
|
-
env_prefix="FASTMCP_",
|
79
|
-
env_file=".env",
|
80
|
-
extra="ignore",
|
81
|
-
)
|
82
|
-
|
83
|
-
# Server settings
|
84
|
-
debug: bool = False
|
85
|
-
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO"
|
86
|
-
|
87
|
-
# HTTP settings
|
88
|
-
host: str = "0.0.0.0"
|
89
|
-
port: int = 8000
|
90
|
-
|
91
|
-
# resource settings
|
92
|
-
warn_on_duplicate_resources: bool = True
|
93
|
-
|
94
|
-
# tool settings
|
95
|
-
warn_on_duplicate_tools: bool = True
|
96
|
-
|
97
|
-
# prompt settings
|
98
|
-
warn_on_duplicate_prompts: bool = True
|
99
|
-
|
100
|
-
# agent settings
|
101
|
-
warn_on_duplicate_agents: bool = True
|
102
|
-
|
103
|
-
dependencies: list[str] = Field(
|
104
|
-
default_factory=list,
|
105
|
-
description="List of dependencies to install in the server environment",
|
106
|
-
)
|
107
|
-
|
108
|
-
|
109
|
-
class Server:
|
110
|
-
def __init__(
|
111
|
-
self, name: str | None = None, instructions: str | None = None, **settings: Any
|
112
|
-
):
|
113
|
-
self.settings = Settings(**settings)
|
114
|
-
self._mcp_server = MCPServer(name=name or "FastMCP", instructions=instructions)
|
115
|
-
self._tool_manager = ToolManager(
|
116
|
-
warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools
|
117
|
-
)
|
118
|
-
self._resource_manager = ResourceManager(
|
119
|
-
warn_on_duplicate_resources=self.settings.warn_on_duplicate_resources
|
120
|
-
)
|
121
|
-
self._prompt_manager = PromptManager(
|
122
|
-
warn_on_duplicate_prompts=self.settings.warn_on_duplicate_prompts
|
123
|
-
)
|
124
|
-
self._agent_manager = AgentManager(
|
125
|
-
warn_on_duplicate_agents=self.settings.warn_on_duplicate_agents
|
126
|
-
)
|
127
|
-
self.dependencies = self.settings.dependencies
|
128
|
-
|
129
|
-
# Set up MCP protocol handlers
|
130
|
-
self._setup_handlers()
|
131
|
-
|
132
|
-
# Configure logging
|
133
|
-
configure_logging(self.settings.log_level)
|
134
|
-
|
135
|
-
@property
|
136
|
-
def name(self) -> str:
|
137
|
-
return self._mcp_server.name
|
138
|
-
|
139
|
-
@property
|
140
|
-
def instructions(self) -> str | None:
|
141
|
-
return self._mcp_server.instructions
|
142
|
-
|
143
|
-
def run(self, transport: Literal["stdio", "sse"] = "stdio") -> None:
|
144
|
-
"""Run the FastMCP server. Note this is a synchronous function.
|
145
|
-
|
146
|
-
Args:
|
147
|
-
transport: Transport protocol to use ("stdio" or "sse")
|
148
|
-
"""
|
149
|
-
TRANSPORTS = Literal["stdio", "sse"]
|
150
|
-
if transport not in TRANSPORTS.__args__: # type: ignore
|
151
|
-
raise ValueError(f"Unknown transport: {transport}")
|
152
|
-
|
153
|
-
if transport == "stdio":
|
154
|
-
anyio.run(self.run_stdio_async)
|
155
|
-
else: # transport == "sse"
|
156
|
-
anyio.run(self.run_sse_async)
|
157
|
-
|
158
|
-
def _setup_handlers(self) -> None:
|
159
|
-
"""Set up core MCP protocol handlers."""
|
160
|
-
self._mcp_server.list_tools()(self.list_tools)
|
161
|
-
self._mcp_server.call_tool()(self.call_tool)
|
162
|
-
self._mcp_server.list_resources()(self.list_resources)
|
163
|
-
self._mcp_server.read_resource()(self.read_resource)
|
164
|
-
self._mcp_server.list_prompts()(self.list_prompts)
|
165
|
-
self._mcp_server.get_prompt()(self.get_prompt)
|
166
|
-
self._mcp_server.list_resource_templates()(self.list_resource_templates)
|
167
|
-
self._mcp_server.list_agent_templates()(self.list_agent_templates)
|
168
|
-
self._mcp_server.list_agents()(self.list_agents)
|
169
|
-
self._mcp_server.create_agent()(self.create_agent)
|
170
|
-
self._mcp_server.destroy_agent()(self.destroy_agent)
|
171
|
-
self._mcp_server.run_agent()(self.run_agent)
|
172
|
-
|
173
|
-
async def list_tools(self) -> list[MCPTool]:
|
174
|
-
"""List all available tools."""
|
175
|
-
tools = self._tool_manager.list_tools()
|
176
|
-
return [
|
177
|
-
MCPTool(
|
178
|
-
name=info.name,
|
179
|
-
description=info.description,
|
180
|
-
inputSchema=info.parameters,
|
181
|
-
)
|
182
|
-
for info in tools
|
183
|
-
]
|
184
|
-
|
185
|
-
def get_context(self) -> Context:
|
186
|
-
"""
|
187
|
-
Returns a Context object. Note that the context will only be valid
|
188
|
-
during a request; outside a request, most methods will error.
|
189
|
-
"""
|
190
|
-
try:
|
191
|
-
request_context = self._mcp_server.request_context
|
192
|
-
except LookupError:
|
193
|
-
request_context = None
|
194
|
-
return Context(request_context=request_context, fastmcp=self)
|
195
|
-
|
196
|
-
async def call_tool(
|
197
|
-
self, name: str, arguments: dict[str, Any]
|
198
|
-
) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
|
199
|
-
"""Call a tool by name with arguments."""
|
200
|
-
context = self.get_context()
|
201
|
-
result = await self._tool_manager.call_tool(name, arguments, context=context)
|
202
|
-
converted_result = _convert_to_content(result)
|
203
|
-
return converted_result
|
204
|
-
|
205
|
-
async def list_resources(self) -> list[MCPResource]:
|
206
|
-
"""List all available resources."""
|
207
|
-
|
208
|
-
resources = self._resource_manager.list_resources()
|
209
|
-
return [
|
210
|
-
MCPResource(
|
211
|
-
uri=resource.uri,
|
212
|
-
name=resource.name or "",
|
213
|
-
description=resource.description,
|
214
|
-
mimeType=resource.mime_type,
|
215
|
-
)
|
216
|
-
for resource in resources
|
217
|
-
]
|
218
|
-
|
219
|
-
async def list_resource_templates(self) -> list[MCPResourceTemplate]:
|
220
|
-
templates = self._resource_manager.list_templates()
|
221
|
-
return [
|
222
|
-
MCPResourceTemplate(
|
223
|
-
uriTemplate=template.uri_template,
|
224
|
-
name=template.name,
|
225
|
-
description=template.description,
|
226
|
-
)
|
227
|
-
for template in templates
|
228
|
-
]
|
229
|
-
|
230
|
-
async def read_resource(self, uri: AnyUrl | str) -> ReadResourceContents:
|
231
|
-
"""Read a resource by URI."""
|
232
|
-
|
233
|
-
resource = await self._resource_manager.get_resource(uri)
|
234
|
-
if not resource:
|
235
|
-
raise ResourceError(f"Unknown resource: {uri}")
|
236
|
-
|
237
|
-
try:
|
238
|
-
content = await resource.read()
|
239
|
-
return ReadResourceContents(content=content, mime_type=resource.mime_type)
|
240
|
-
except Exception as e:
|
241
|
-
logger.error(f"Error reading resource {uri}: {e}")
|
242
|
-
raise ResourceError(str(e))
|
243
|
-
|
244
|
-
def add_tool(
|
245
|
-
self,
|
246
|
-
fn: AnyFunction,
|
247
|
-
name: str | None = None,
|
248
|
-
description: str | None = None,
|
249
|
-
) -> None:
|
250
|
-
"""Add a tool to the server.
|
251
|
-
|
252
|
-
The tool function can optionally request a Context object by adding a parameter
|
253
|
-
with the Context type annotation. See the @tool decorator for examples.
|
254
|
-
|
255
|
-
Args:
|
256
|
-
fn: The function to register as a tool
|
257
|
-
name: Optional name for the tool (defaults to function name)
|
258
|
-
description: Optional description of what the tool does
|
259
|
-
"""
|
260
|
-
self._tool_manager.add_tool(fn, name=name, description=description)
|
261
|
-
|
262
|
-
def tool(
|
263
|
-
self, name: str | None = None, description: str | None = None
|
264
|
-
) -> Callable[[AnyFunction], AnyFunction]:
|
265
|
-
"""Decorator to register a tool.
|
266
|
-
|
267
|
-
Tools can optionally request a Context object by adding a parameter with the
|
268
|
-
Context type annotation. The context provides access to MCP capabilities like
|
269
|
-
logging, progress reporting, and resource access.
|
270
|
-
|
271
|
-
Args:
|
272
|
-
name: Optional name for the tool (defaults to function name)
|
273
|
-
description: Optional description of what the tool does
|
274
|
-
|
275
|
-
Example:
|
276
|
-
@server.tool()
|
277
|
-
def my_tool(x: int) -> str:
|
278
|
-
return str(x)
|
279
|
-
|
280
|
-
@server.tool()
|
281
|
-
def tool_with_context(x: int, ctx: Context) -> str:
|
282
|
-
ctx.info(f"Processing {x}")
|
283
|
-
return str(x)
|
284
|
-
|
285
|
-
@server.tool()
|
286
|
-
async def async_tool(x: int, context: Context) -> str:
|
287
|
-
await context.report_progress(50, 100)
|
288
|
-
return str(x)
|
289
|
-
"""
|
290
|
-
# Check if user passed function directly instead of calling decorator
|
291
|
-
if callable(name):
|
292
|
-
raise TypeError(
|
293
|
-
"The @tool decorator was used incorrectly. "
|
294
|
-
"Did you forget to call it? Use @tool() instead of @tool"
|
295
|
-
)
|
296
|
-
|
297
|
-
def decorator(fn: AnyFunction) -> AnyFunction:
|
298
|
-
self.add_tool(fn, name=name, description=description)
|
299
|
-
return fn
|
300
|
-
|
301
|
-
return decorator
|
302
|
-
|
303
|
-
def add_resource(self, resource: Resource) -> None:
|
304
|
-
"""Add a resource to the server.
|
305
|
-
|
306
|
-
Args:
|
307
|
-
resource: A Resource instance to add
|
308
|
-
"""
|
309
|
-
self._resource_manager.add_resource(resource)
|
310
|
-
|
311
|
-
def resource(
|
312
|
-
self,
|
313
|
-
uri: str,
|
314
|
-
*,
|
315
|
-
name: str | None = None,
|
316
|
-
description: str | None = None,
|
317
|
-
mime_type: str | None = None,
|
318
|
-
) -> Callable[[AnyFunction], AnyFunction]:
|
319
|
-
"""Decorator to register a function as a resource.
|
320
|
-
|
321
|
-
The function will be called when the resource is read to generate its content.
|
322
|
-
The function can return:
|
323
|
-
- str for text content
|
324
|
-
- bytes for binary content
|
325
|
-
- other types will be converted to JSON
|
326
|
-
|
327
|
-
If the URI contains parameters (e.g. "resource://{param}") or the function
|
328
|
-
has parameters, it will be registered as a template resource.
|
329
|
-
|
330
|
-
Args:
|
331
|
-
uri: URI for the resource (e.g. "resource://my-resource" or "resource://{param}")
|
332
|
-
name: Optional name for the resource
|
333
|
-
description: Optional description of the resource
|
334
|
-
mime_type: Optional MIME type for the resource
|
335
|
-
|
336
|
-
Example:
|
337
|
-
@server.resource("resource://my-resource")
|
338
|
-
def get_data() -> str:
|
339
|
-
return "Hello, world!"
|
340
|
-
|
341
|
-
@server.resource("resource://my-resource")
|
342
|
-
async get_data() -> str:
|
343
|
-
data = await fetch_data()
|
344
|
-
return f"Hello, world! {data}"
|
345
|
-
|
346
|
-
@server.resource("resource://{city}/weather")
|
347
|
-
def get_weather(city: str) -> str:
|
348
|
-
return f"Weather for {city}"
|
349
|
-
|
350
|
-
@server.resource("resource://{city}/weather")
|
351
|
-
async def get_weather(city: str) -> str:
|
352
|
-
data = await fetch_weather(city)
|
353
|
-
return f"Weather for {city}: {data}"
|
354
|
-
"""
|
355
|
-
# Check if user passed function directly instead of calling decorator
|
356
|
-
if callable(uri):
|
357
|
-
raise TypeError(
|
358
|
-
"The @resource decorator was used incorrectly. "
|
359
|
-
"Did you forget to call it? Use @resource('uri') instead of @resource"
|
360
|
-
)
|
361
|
-
|
362
|
-
def decorator(fn: AnyFunction) -> AnyFunction:
|
363
|
-
# Check if this should be a template
|
364
|
-
has_uri_params = "{" in uri and "}" in uri
|
365
|
-
has_func_params = bool(inspect.signature(fn).parameters)
|
366
|
-
|
367
|
-
if has_uri_params or has_func_params:
|
368
|
-
# Validate that URI params match function params
|
369
|
-
uri_params = set(re.findall(r"{(\w+)}", uri))
|
370
|
-
func_params = set(inspect.signature(fn).parameters.keys())
|
371
|
-
|
372
|
-
if uri_params != func_params:
|
373
|
-
raise ValueError(
|
374
|
-
f"Mismatch between URI parameters {uri_params} "
|
375
|
-
f"and function parameters {func_params}"
|
376
|
-
)
|
377
|
-
|
378
|
-
# Register as template
|
379
|
-
self._resource_manager.add_template(
|
380
|
-
fn=fn,
|
381
|
-
uri_template=uri,
|
382
|
-
name=name,
|
383
|
-
description=description,
|
384
|
-
mime_type=mime_type or "text/plain",
|
385
|
-
)
|
386
|
-
else:
|
387
|
-
# Register as regular resource
|
388
|
-
resource = FunctionResource(
|
389
|
-
uri=AnyUrl(uri),
|
390
|
-
name=name,
|
391
|
-
description=description,
|
392
|
-
mime_type=mime_type or "text/plain",
|
393
|
-
fn=fn,
|
394
|
-
)
|
395
|
-
self.add_resource(resource)
|
396
|
-
return fn
|
397
|
-
|
398
|
-
return decorator
|
399
|
-
|
400
|
-
def add_prompt(self, prompt: Prompt) -> None:
|
401
|
-
"""Add a prompt to the server.
|
402
|
-
|
403
|
-
Args:
|
404
|
-
prompt: A Prompt instance to add
|
405
|
-
"""
|
406
|
-
self._prompt_manager.add_prompt(prompt)
|
407
|
-
|
408
|
-
def prompt(
|
409
|
-
self, name: str | None = None, description: str | None = None
|
410
|
-
) -> Callable[[AnyFunction], AnyFunction]:
|
411
|
-
"""Decorator to register a prompt.
|
412
|
-
|
413
|
-
Args:
|
414
|
-
name: Optional name for the prompt (defaults to function name)
|
415
|
-
description: Optional description of what the prompt does
|
416
|
-
|
417
|
-
Example:
|
418
|
-
@server.prompt()
|
419
|
-
def analyze_table(table_name: str) -> list[Message]:
|
420
|
-
schema = read_table_schema(table_name)
|
421
|
-
return [
|
422
|
-
{
|
423
|
-
"role": "user",
|
424
|
-
"content": f"Analyze this schema:\n{schema}"
|
425
|
-
}
|
426
|
-
]
|
427
|
-
|
428
|
-
@server.prompt()
|
429
|
-
async def analyze_file(path: str) -> list[Message]:
|
430
|
-
content = await read_file(path)
|
431
|
-
return [
|
432
|
-
{
|
433
|
-
"role": "user",
|
434
|
-
"content": {
|
435
|
-
"type": "resource",
|
436
|
-
"resource": {
|
437
|
-
"uri": f"file://{path}",
|
438
|
-
"text": content
|
439
|
-
}
|
440
|
-
}
|
441
|
-
}
|
442
|
-
]
|
443
|
-
"""
|
444
|
-
# Check if user passed function directly instead of calling decorator
|
445
|
-
if callable(name):
|
446
|
-
raise TypeError(
|
447
|
-
"The @prompt decorator was used incorrectly. "
|
448
|
-
"Did you forget to call it? Use @prompt() instead of @prompt"
|
449
|
-
)
|
450
|
-
|
451
|
-
def decorator(func: AnyFunction) -> AnyFunction:
|
452
|
-
prompt = Prompt.from_function(func, name=name, description=description)
|
453
|
-
self.add_prompt(prompt)
|
454
|
-
return func
|
455
|
-
|
456
|
-
return decorator
|
457
|
-
|
458
|
-
def add_agent_template(self, template: AgentTemplate) -> None:
|
459
|
-
"""Add a agent to the server."""
|
460
|
-
|
461
|
-
self._agent_manager.add_template(template=template)
|
462
|
-
|
463
|
-
def agent_template(
|
464
|
-
self,
|
465
|
-
name: str,
|
466
|
-
description: str,
|
467
|
-
config: Type[BaseModel],
|
468
|
-
input: Type[BaseModel],
|
469
|
-
output: Type[BaseModel],
|
470
|
-
**kwargs,
|
471
|
-
) -> Callable:
|
472
|
-
"""Decorator to register an agent template.
|
473
|
-
|
474
|
-
Args:
|
475
|
-
name: name for the agent
|
476
|
-
description: description of what the agent does
|
477
|
-
config: agent configuration model
|
478
|
-
input: agent run input model
|
479
|
-
output: agent run output model
|
480
|
-
"""
|
481
|
-
# Check if user passed function directly instead of calling decorator
|
482
|
-
if callable(name):
|
483
|
-
raise TypeError(
|
484
|
-
"The @agent_template decorator was used incorrectly. "
|
485
|
-
"Did you forget to call it? Use @agent_template()"
|
486
|
-
+ " instead of @agent_template"
|
487
|
-
)
|
488
|
-
|
489
|
-
def decorator(func: Callable) -> Callable:
|
490
|
-
template = AgentTemplate(
|
491
|
-
name=name,
|
492
|
-
description=description,
|
493
|
-
config=config,
|
494
|
-
input=input,
|
495
|
-
output=output,
|
496
|
-
create_fn=func,
|
497
|
-
**kwargs,
|
498
|
-
)
|
499
|
-
self.add_agent_template(template)
|
500
|
-
return func
|
501
|
-
|
502
|
-
return decorator
|
503
|
-
|
504
|
-
async def list_agent_templates(
|
505
|
-
self, req: ListAgentTemplatesRequest
|
506
|
-
) -> ListAgentTemplatesResult:
|
507
|
-
templates = self._agent_manager.list_templates()
|
508
|
-
return ListAgentTemplatesResult(
|
509
|
-
agentTemplates=[
|
510
|
-
MCPAgentTemplate(
|
511
|
-
name=template.name,
|
512
|
-
description=template.description,
|
513
|
-
configSchema=template.config.model_json_schema(),
|
514
|
-
inputSchema=template.input.model_json_schema(),
|
515
|
-
outputSchema=template.output.model_json_schema(),
|
516
|
-
**(template.model_extra if template.model_extra else {}),
|
517
|
-
)
|
518
|
-
for template in templates
|
519
|
-
]
|
520
|
-
)
|
521
|
-
|
522
|
-
def add_agent(self, agent: Agent) -> None:
|
523
|
-
"""Add a agent to the server."""
|
524
|
-
self._agent_manager.add_agent(agent=agent)
|
525
|
-
|
526
|
-
def agent(
|
527
|
-
self,
|
528
|
-
name: str,
|
529
|
-
description: str,
|
530
|
-
input: Type[BaseModel],
|
531
|
-
output: Type[BaseModel],
|
532
|
-
**kwargs,
|
533
|
-
) -> Callable:
|
534
|
-
"""Decorator to register an agent.
|
535
|
-
|
536
|
-
Args:
|
537
|
-
name: name for the agent
|
538
|
-
description: description of what the agent does
|
539
|
-
input: agent run input model
|
540
|
-
output: agent run output model
|
541
|
-
"""
|
542
|
-
# Check if user passed function directly instead of calling decorator
|
543
|
-
if callable(name):
|
544
|
-
raise TypeError(
|
545
|
-
"The @agent decorator was used incorrectly. "
|
546
|
-
"Did you forget to call it? Use @agent() instead of @agent"
|
547
|
-
)
|
548
|
-
|
549
|
-
def decorator(func: Callable) -> Callable:
|
550
|
-
agent = Agent(
|
551
|
-
name=name,
|
552
|
-
description=description,
|
553
|
-
input=input,
|
554
|
-
output=output,
|
555
|
-
run_fn=func,
|
556
|
-
destroy_fn=None,
|
557
|
-
**kwargs,
|
558
|
-
)
|
559
|
-
self.add_agent(agent=agent)
|
560
|
-
return func
|
561
|
-
|
562
|
-
return decorator
|
563
|
-
|
564
|
-
async def list_agents(self, req: ListAgentsRequest) -> ListAgentsResult:
|
565
|
-
agents = self._agent_manager.list_agents()
|
566
|
-
return ListAgentsResult(
|
567
|
-
agents=[
|
568
|
-
MCPAgent(
|
569
|
-
name=agent.name,
|
570
|
-
description=agent.description,
|
571
|
-
inputSchema=agent.input.model_json_schema(),
|
572
|
-
outputSchema=agent.output.model_json_schema(),
|
573
|
-
**(agent.model_extra if agent.model_extra else {}),
|
574
|
-
)
|
575
|
-
for agent in agents
|
576
|
-
]
|
577
|
-
)
|
578
|
-
|
579
|
-
async def create_agent(self, req: CreateAgentRequest) -> CreateAgentResult:
|
580
|
-
agent = await self._agent_manager.create_agent(
|
581
|
-
name=req.params.templateName,
|
582
|
-
config=req.params.config,
|
583
|
-
context=self.get_context(),
|
584
|
-
)
|
585
|
-
return CreateAgentResult(
|
586
|
-
agent=MCPAgent(
|
587
|
-
name=agent.name,
|
588
|
-
description=agent.description,
|
589
|
-
inputSchema=agent.input.model_json_schema(),
|
590
|
-
outputSchema=agent.output.model_json_schema(),
|
591
|
-
)
|
592
|
-
)
|
593
|
-
|
594
|
-
async def destroy_agent(self, req: DestroyAgentRequest) -> DestroyAgentResult:
|
595
|
-
await self._agent_manager.destroy_agent(
|
596
|
-
name=req.params.name, context=self.get_context()
|
597
|
-
)
|
598
|
-
return DestroyAgentResult()
|
599
|
-
|
600
|
-
async def run_agent(self, req: RunAgentRequest) -> RunAgentResult:
|
601
|
-
"""Run an agent by name with arguments."""
|
602
|
-
output = await self._agent_manager.run_agent(
|
603
|
-
name=req.params.name, input=req.params.input, context=self.get_context()
|
604
|
-
)
|
605
|
-
return RunAgentResult(output=output)
|
606
|
-
|
607
|
-
async def run_stdio_async(self) -> None:
|
608
|
-
"""Run the server using stdio transport."""
|
609
|
-
async with stdio_server() as (read_stream, write_stream):
|
610
|
-
await self._mcp_server.run(
|
611
|
-
read_stream,
|
612
|
-
write_stream,
|
613
|
-
self._mcp_server.create_initialization_options(),
|
614
|
-
)
|
615
|
-
|
616
|
-
async def run_sse_async(self, **uvicorn_kwargs) -> None:
|
617
|
-
"""Run the server using SSE transport."""
|
618
|
-
from starlette.applications import Starlette
|
619
|
-
from starlette.routing import Mount, Route
|
620
|
-
|
621
|
-
sse = SseServerTransport("/messages/")
|
622
|
-
|
623
|
-
async def handle_sse(request):
|
624
|
-
async with sse.connect_sse(
|
625
|
-
request.scope, request.receive, request._send
|
626
|
-
) as streams:
|
627
|
-
await self._mcp_server.run(
|
628
|
-
streams[0],
|
629
|
-
streams[1],
|
630
|
-
self._mcp_server.create_initialization_options(),
|
631
|
-
)
|
632
|
-
|
633
|
-
starlette_app = Starlette(
|
634
|
-
debug=self.settings.debug,
|
635
|
-
routes=[
|
636
|
-
Route("/sse", endpoint=handle_sse),
|
637
|
-
Mount("/messages/", app=sse.handle_post_message),
|
638
|
-
],
|
639
|
-
)
|
640
|
-
|
641
|
-
config = uvicorn.Config(
|
642
|
-
starlette_app,
|
643
|
-
host=self.settings.host,
|
644
|
-
port=self.settings.port,
|
645
|
-
log_level=self.settings.log_level.lower(),
|
646
|
-
**uvicorn_kwargs,
|
647
|
-
)
|
648
|
-
server = uvicorn.Server(config)
|
649
|
-
await server.serve()
|
650
|
-
|
651
|
-
async def list_prompts(self) -> list[MCPPrompt]:
|
652
|
-
"""List all available prompts."""
|
653
|
-
prompts = self._prompt_manager.list_prompts()
|
654
|
-
return [
|
655
|
-
MCPPrompt(
|
656
|
-
name=prompt.name,
|
657
|
-
description=prompt.description,
|
658
|
-
arguments=[
|
659
|
-
MCPPromptArgument(
|
660
|
-
name=arg.name,
|
661
|
-
description=arg.description,
|
662
|
-
required=arg.required,
|
663
|
-
)
|
664
|
-
for arg in (prompt.arguments or [])
|
665
|
-
],
|
666
|
-
)
|
667
|
-
for prompt in prompts
|
668
|
-
]
|
669
|
-
|
670
|
-
async def get_prompt(
|
671
|
-
self, name: str, arguments: dict[str, Any] | None = None
|
672
|
-
) -> GetPromptResult:
|
673
|
-
"""Get a prompt by name with arguments."""
|
674
|
-
try:
|
675
|
-
messages = await self._prompt_manager.render_prompt(name, arguments)
|
676
|
-
|
677
|
-
return GetPromptResult(messages=pydantic_core.to_jsonable_python(messages))
|
678
|
-
except Exception as e:
|
679
|
-
logger.error(f"Error getting prompt {name}: {e}")
|
680
|
-
raise ValueError(str(e))
|
681
|
-
|
682
|
-
|
683
|
-
def _convert_to_content(
|
684
|
-
result: Any,
|
685
|
-
) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
|
686
|
-
"""Convert a result to a sequence of content objects."""
|
687
|
-
if result is None:
|
688
|
-
return []
|
689
|
-
|
690
|
-
if isinstance(result, (TextContent, ImageContent, EmbeddedResource)):
|
691
|
-
return [result]
|
692
|
-
|
693
|
-
if isinstance(result, Image):
|
694
|
-
return [result.to_image_content()]
|
695
|
-
|
696
|
-
if isinstance(result, (list, tuple)):
|
697
|
-
return list(chain.from_iterable(_convert_to_content(item) for item in result))
|
698
|
-
|
699
|
-
if not isinstance(result, str):
|
700
|
-
try:
|
701
|
-
result = json.dumps(pydantic_core.to_jsonable_python(result))
|
702
|
-
except Exception:
|
703
|
-
result = str(result)
|
704
|
-
|
705
|
-
return [TextContent(type="text", text=result)]
|