fastmcp 1.0__py3-none-any.whl → 2.1.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.
- fastmcp/__init__.py +15 -4
- fastmcp/cli/__init__.py +0 -1
- fastmcp/cli/claude.py +13 -11
- fastmcp/cli/cli.py +59 -39
- fastmcp/client/__init__.py +25 -0
- fastmcp/client/base.py +1 -0
- fastmcp/client/client.py +226 -0
- fastmcp/client/roots.py +75 -0
- fastmcp/client/sampling.py +50 -0
- fastmcp/client/transports.py +411 -0
- fastmcp/prompts/__init__.py +2 -2
- fastmcp/prompts/{base.py → prompt.py} +47 -26
- fastmcp/prompts/prompt_manager.py +69 -15
- fastmcp/resources/__init__.py +6 -6
- fastmcp/resources/{base.py → resource.py} +21 -2
- fastmcp/resources/resource_manager.py +116 -17
- fastmcp/resources/{templates.py → template.py} +36 -11
- fastmcp/resources/types.py +18 -13
- fastmcp/server/__init__.py +5 -0
- fastmcp/server/context.py +222 -0
- fastmcp/server/openapi.py +637 -0
- fastmcp/server/proxy.py +223 -0
- fastmcp/{server.py → server/server.py} +323 -267
- fastmcp/settings.py +81 -0
- fastmcp/tools/__init__.py +1 -1
- fastmcp/tools/{base.py → tool.py} +47 -18
- fastmcp/tools/tool_manager.py +57 -16
- fastmcp/utilities/func_metadata.py +33 -19
- fastmcp/utilities/openapi.py +797 -0
- fastmcp/utilities/types.py +15 -4
- fastmcp-2.1.0.dist-info/METADATA +770 -0
- fastmcp-2.1.0.dist-info/RECORD +39 -0
- fastmcp-2.1.0.dist-info/licenses/LICENSE +201 -0
- fastmcp/prompts/manager.py +0 -50
- fastmcp-1.0.dist-info/METADATA +0 -604
- fastmcp-1.0.dist-info/RECORD +0 -28
- fastmcp-1.0.dist-info/licenses/LICENSE +0 -21
- {fastmcp-1.0.dist-info → fastmcp-2.1.0.dist-info}/WHEEL +0 -0
- {fastmcp-1.0.dist-info → fastmcp-2.1.0.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
from __future__ import annotations as _annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Generic, Literal
|
|
4
|
+
|
|
5
|
+
from mcp.server.lowlevel.helper_types import ReadResourceContents
|
|
6
|
+
from mcp.server.session import ServerSessionT
|
|
7
|
+
from mcp.shared.context import LifespanContextT, RequestContext
|
|
8
|
+
from mcp.types import (
|
|
9
|
+
CreateMessageResult,
|
|
10
|
+
ImageContent,
|
|
11
|
+
Root,
|
|
12
|
+
SamplingMessage,
|
|
13
|
+
TextContent,
|
|
14
|
+
)
|
|
15
|
+
from pydantic import BaseModel
|
|
16
|
+
from pydantic.networks import AnyUrl
|
|
17
|
+
|
|
18
|
+
from fastmcp.server.server import FastMCP
|
|
19
|
+
from fastmcp.utilities.logging import get_logger
|
|
20
|
+
|
|
21
|
+
logger = get_logger(__name__)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Context(BaseModel, Generic[ServerSessionT, LifespanContextT]):
|
|
25
|
+
"""Context object providing access to MCP capabilities.
|
|
26
|
+
|
|
27
|
+
This provides a cleaner interface to MCP's RequestContext functionality.
|
|
28
|
+
It gets injected into tool and resource functions that request it via type hints.
|
|
29
|
+
|
|
30
|
+
To use context in a tool function, add a parameter with the Context type annotation:
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
@server.tool()
|
|
34
|
+
def my_tool(x: int, ctx: Context) -> str:
|
|
35
|
+
# Log messages to the client
|
|
36
|
+
ctx.info(f"Processing {x}")
|
|
37
|
+
ctx.debug("Debug info")
|
|
38
|
+
ctx.warning("Warning message")
|
|
39
|
+
ctx.error("Error message")
|
|
40
|
+
|
|
41
|
+
# Report progress
|
|
42
|
+
ctx.report_progress(50, 100)
|
|
43
|
+
|
|
44
|
+
# Access resources
|
|
45
|
+
data = ctx.read_resource("resource://data")
|
|
46
|
+
|
|
47
|
+
# Get request info
|
|
48
|
+
request_id = ctx.request_id
|
|
49
|
+
client_id = ctx.client_id
|
|
50
|
+
|
|
51
|
+
return str(x)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The context parameter name can be anything as long as it's annotated with Context.
|
|
55
|
+
The context is optional - tools that don't need it can omit the parameter.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
_request_context: RequestContext[ServerSessionT, LifespanContextT] | None
|
|
59
|
+
_fastmcp: FastMCP | None
|
|
60
|
+
|
|
61
|
+
def __init__(
|
|
62
|
+
self,
|
|
63
|
+
*,
|
|
64
|
+
request_context: RequestContext[ServerSessionT, LifespanContextT] | None = None,
|
|
65
|
+
fastmcp: FastMCP | None = None,
|
|
66
|
+
**kwargs: Any,
|
|
67
|
+
):
|
|
68
|
+
super().__init__(**kwargs)
|
|
69
|
+
self._request_context = request_context
|
|
70
|
+
self._fastmcp = fastmcp
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def fastmcp(self) -> FastMCP:
|
|
74
|
+
"""Access to the FastMCP server."""
|
|
75
|
+
if self._fastmcp is None:
|
|
76
|
+
raise ValueError("Context is not available outside of a request")
|
|
77
|
+
return self._fastmcp
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def request_context(self) -> RequestContext[ServerSessionT, LifespanContextT]:
|
|
81
|
+
"""Access to the underlying request context."""
|
|
82
|
+
if self._request_context is None:
|
|
83
|
+
raise ValueError("Context is not available outside of a request")
|
|
84
|
+
return self._request_context
|
|
85
|
+
|
|
86
|
+
async def report_progress(
|
|
87
|
+
self, progress: float, total: float | None = None
|
|
88
|
+
) -> None:
|
|
89
|
+
"""Report progress for the current operation.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
progress: Current progress value e.g. 24
|
|
93
|
+
total: Optional total value e.g. 100
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
progress_token = (
|
|
97
|
+
self.request_context.meta.progressToken
|
|
98
|
+
if self.request_context.meta
|
|
99
|
+
else None
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if progress_token is None:
|
|
103
|
+
return
|
|
104
|
+
|
|
105
|
+
await self.request_context.session.send_progress_notification(
|
|
106
|
+
progress_token=progress_token, progress=progress, total=total
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
async def read_resource(self, uri: str | AnyUrl) -> list[ReadResourceContents]:
|
|
110
|
+
"""Read a resource by URI.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
uri: Resource URI to read
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
The resource content as either text or bytes
|
|
117
|
+
"""
|
|
118
|
+
assert self._fastmcp is not None, (
|
|
119
|
+
"Context is not available outside of a request"
|
|
120
|
+
)
|
|
121
|
+
return await self._fastmcp.read_resource(uri)
|
|
122
|
+
|
|
123
|
+
async def log(
|
|
124
|
+
self,
|
|
125
|
+
level: Literal["debug", "info", "warning", "error"],
|
|
126
|
+
message: str,
|
|
127
|
+
*,
|
|
128
|
+
logger_name: str | None = None,
|
|
129
|
+
) -> None:
|
|
130
|
+
"""Send a log message to the client.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
level: Log level (debug, info, warning, error)
|
|
134
|
+
message: Log message
|
|
135
|
+
logger_name: Optional logger name
|
|
136
|
+
**extra: Additional structured data to include
|
|
137
|
+
"""
|
|
138
|
+
await self.request_context.session.send_log_message(
|
|
139
|
+
level=level, data=message, logger=logger_name
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def client_id(self) -> str | None:
|
|
144
|
+
"""Get the client ID if available."""
|
|
145
|
+
return (
|
|
146
|
+
getattr(self.request_context.meta, "client_id", None)
|
|
147
|
+
if self.request_context.meta
|
|
148
|
+
else None
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def request_id(self) -> str:
|
|
153
|
+
"""Get the unique ID for this request."""
|
|
154
|
+
return str(self.request_context.request_id)
|
|
155
|
+
|
|
156
|
+
@property
|
|
157
|
+
def session(self):
|
|
158
|
+
"""Access to the underlying session for advanced usage."""
|
|
159
|
+
return self.request_context.session
|
|
160
|
+
|
|
161
|
+
# Convenience methods for common log levels
|
|
162
|
+
async def debug(self, message: str, **extra: Any) -> None:
|
|
163
|
+
"""Send a debug log message."""
|
|
164
|
+
await self.log("debug", message, **extra)
|
|
165
|
+
|
|
166
|
+
async def info(self, message: str, **extra: Any) -> None:
|
|
167
|
+
"""Send an info log message."""
|
|
168
|
+
await self.log("info", message, **extra)
|
|
169
|
+
|
|
170
|
+
async def warning(self, message: str, **extra: Any) -> None:
|
|
171
|
+
"""Send a warning log message."""
|
|
172
|
+
await self.log("warning", message, **extra)
|
|
173
|
+
|
|
174
|
+
async def error(self, message: str, **extra: Any) -> None:
|
|
175
|
+
"""Send an error log message."""
|
|
176
|
+
await self.log("error", message, **extra)
|
|
177
|
+
|
|
178
|
+
async def list_roots(self) -> list[Root]:
|
|
179
|
+
"""List the roots available to the server, as indicated by the client."""
|
|
180
|
+
result = await self.request_context.session.list_roots()
|
|
181
|
+
return result.roots
|
|
182
|
+
|
|
183
|
+
async def sample(
|
|
184
|
+
self,
|
|
185
|
+
messages: str | list[str | SamplingMessage],
|
|
186
|
+
system_prompt: str | None = None,
|
|
187
|
+
temperature: float | None = None,
|
|
188
|
+
max_tokens: int | None = None,
|
|
189
|
+
) -> TextContent | ImageContent:
|
|
190
|
+
"""
|
|
191
|
+
Send a sampling request to the client and await the response.
|
|
192
|
+
|
|
193
|
+
Call this method at any time to have the server request an LLM
|
|
194
|
+
completion from the client. The client must be appropriately configured,
|
|
195
|
+
or the request will error.
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
if max_tokens is None:
|
|
199
|
+
max_tokens = 512
|
|
200
|
+
|
|
201
|
+
if isinstance(messages, str):
|
|
202
|
+
sampling_messages = [
|
|
203
|
+
SamplingMessage(
|
|
204
|
+
content=TextContent(text=messages, type="text"), role="user"
|
|
205
|
+
)
|
|
206
|
+
]
|
|
207
|
+
elif isinstance(messages, list):
|
|
208
|
+
sampling_messages = [
|
|
209
|
+
SamplingMessage(content=TextContent(text=m, type="text"), role="user")
|
|
210
|
+
if isinstance(m, str)
|
|
211
|
+
else m
|
|
212
|
+
for m in messages
|
|
213
|
+
]
|
|
214
|
+
|
|
215
|
+
result: CreateMessageResult = await self.request_context.session.create_message(
|
|
216
|
+
messages=sampling_messages,
|
|
217
|
+
system_prompt=system_prompt,
|
|
218
|
+
temperature=temperature,
|
|
219
|
+
max_tokens=max_tokens,
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
return result.content
|