Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
d895aec
1
Parent(s): be7ad70
Add docstrings
Browse files- src/fastmcp/server.py +61 -2
src/fastmcp/server.py
CHANGED
|
@@ -189,13 +189,45 @@ class FastMCP:
|
|
| 189 |
name: Optional[str] = None,
|
| 190 |
description: Optional[str] = None,
|
| 191 |
) -> None:
|
| 192 |
-
"""Add a tool to the server.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
self._tool_manager.add_tool(func, name=name, description=description)
|
| 194 |
|
| 195 |
def tool(
|
| 196 |
self, name: Optional[str] = None, description: Optional[str] = None
|
| 197 |
) -> Callable:
|
| 198 |
-
"""Decorator to register a tool.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
# Check if user passed function directly instead of calling decorator
|
| 200 |
if callable(name):
|
| 201 |
raise TypeError(
|
|
@@ -392,6 +424,33 @@ class Context(BaseModel):
|
|
| 392 |
|
| 393 |
This provides a cleaner interface to MCP's RequestContext functionality.
|
| 394 |
It gets injected into tool and resource functions that request it via type hints.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
"""
|
| 396 |
|
| 397 |
_request_context: RequestContext
|
|
|
|
| 189 |
name: Optional[str] = None,
|
| 190 |
description: Optional[str] = None,
|
| 191 |
) -> None:
|
| 192 |
+
"""Add a tool to the server.
|
| 193 |
+
|
| 194 |
+
The tool function can optionally request a Context object by adding a parameter
|
| 195 |
+
with the Context type annotation. See the @tool decorator for examples.
|
| 196 |
+
|
| 197 |
+
Args:
|
| 198 |
+
func: The function to register as a tool
|
| 199 |
+
name: Optional name for the tool (defaults to function name)
|
| 200 |
+
description: Optional description of what the tool does
|
| 201 |
+
"""
|
| 202 |
self._tool_manager.add_tool(func, name=name, description=description)
|
| 203 |
|
| 204 |
def tool(
|
| 205 |
self, name: Optional[str] = None, description: Optional[str] = None
|
| 206 |
) -> Callable:
|
| 207 |
+
"""Decorator to register a tool.
|
| 208 |
+
|
| 209 |
+
Tools can optionally request a Context object by adding a parameter with the Context type annotation.
|
| 210 |
+
The context provides access to MCP capabilities like logging, progress reporting, and resource access.
|
| 211 |
+
|
| 212 |
+
Args:
|
| 213 |
+
name: Optional name for the tool (defaults to function name)
|
| 214 |
+
description: Optional description of what the tool does
|
| 215 |
+
|
| 216 |
+
Example:
|
| 217 |
+
@server.tool()
|
| 218 |
+
def my_tool(x: int) -> str:
|
| 219 |
+
return str(x)
|
| 220 |
+
|
| 221 |
+
@server.tool()
|
| 222 |
+
def tool_with_context(x: int, ctx: Context) -> str:
|
| 223 |
+
ctx.info(f"Processing {x}")
|
| 224 |
+
return str(x)
|
| 225 |
+
|
| 226 |
+
@server.tool()
|
| 227 |
+
async def async_tool(x: int, context: Context) -> str:
|
| 228 |
+
await context.report_progress(50, 100)
|
| 229 |
+
return str(x)
|
| 230 |
+
"""
|
| 231 |
# Check if user passed function directly instead of calling decorator
|
| 232 |
if callable(name):
|
| 233 |
raise TypeError(
|
|
|
|
| 424 |
|
| 425 |
This provides a cleaner interface to MCP's RequestContext functionality.
|
| 426 |
It gets injected into tool and resource functions that request it via type hints.
|
| 427 |
+
|
| 428 |
+
To use context in a tool function, add a parameter with the Context type annotation:
|
| 429 |
+
|
| 430 |
+
```python
|
| 431 |
+
@server.tool()
|
| 432 |
+
def my_tool(x: int, ctx: Context) -> str:
|
| 433 |
+
# Log messages to the client
|
| 434 |
+
ctx.info(f"Processing {x}")
|
| 435 |
+
ctx.debug("Debug info")
|
| 436 |
+
ctx.warning("Warning message")
|
| 437 |
+
ctx.error("Error message")
|
| 438 |
+
|
| 439 |
+
# Report progress
|
| 440 |
+
ctx.report_progress(50, 100)
|
| 441 |
+
|
| 442 |
+
# Access resources
|
| 443 |
+
data = ctx.read_resource("resource://data")
|
| 444 |
+
|
| 445 |
+
# Get request info
|
| 446 |
+
request_id = ctx.request_id
|
| 447 |
+
client_id = ctx.client_id
|
| 448 |
+
|
| 449 |
+
return str(x)
|
| 450 |
+
```
|
| 451 |
+
|
| 452 |
+
The context parameter name can be anything as long as it's annotated with Context.
|
| 453 |
+
The context is optional - tools that don't need it can omit the parameter.
|
| 454 |
"""
|
| 455 |
|
| 456 |
_request_context: RequestContext
|