Spaces:
Running
Running
Add moree docs
Browse files
src/fastmcp/contrib/mcp_mixin/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
# MCP Mixin
|
| 2 |
|
| 3 |
This module provides the `MCPMixin` base class and associated decorators (`@mcp_tool`, `@mcp_resource`, `@mcp_prompt`).
|
|
@@ -21,8 +23,9 @@ Resources:
|
|
| 21 |
Inherit from `MCPMixin` and use the decorators on the methods you want to register.
|
| 22 |
|
| 23 |
```python
|
|
|
|
| 24 |
from fastmcp import FastMCP
|
| 25 |
-
from fastmcp.contrib.mcp_mixin import MCPMixin, mcp_tool, mcp_resource
|
| 26 |
|
| 27 |
class MyComponent(MCPMixin):
|
| 28 |
@mcp_tool(name="my_tool", description="Does something cool.")
|
|
@@ -35,18 +38,47 @@ class MyComponent(MCPMixin):
|
|
| 35 |
# This function can't be called by client because it's disabled
|
| 36 |
return "You'll never get here!"
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
@mcp_tool(
|
| 39 |
name="my_tool", description="Does something cool.",
|
| 40 |
-
annotations=
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
)
|
| 47 |
def tool_method(self):
|
| 48 |
return "Tool executed!"
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
@mcp_resource(uri="component://data")
|
| 51 |
def resource_method(self):
|
| 52 |
return {"data": "some data"}
|
|
@@ -56,6 +88,16 @@ class MyComponent(MCPMixin):
|
|
| 56 |
def resource_method(self):
|
| 57 |
return {"data": "some data"}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
mcp_server = FastMCP()
|
| 60 |
component = MyComponent()
|
| 61 |
|
|
|
|
| 1 |
+
from mcp.types import ToolAnnotations
|
| 2 |
+
|
| 3 |
# MCP Mixin
|
| 4 |
|
| 5 |
This module provides the `MCPMixin` base class and associated decorators (`@mcp_tool`, `@mcp_resource`, `@mcp_prompt`).
|
|
|
|
| 23 |
Inherit from `MCPMixin` and use the decorators on the methods you want to register.
|
| 24 |
|
| 25 |
```python
|
| 26 |
+
from mcp.types import ToolAnnotations
|
| 27 |
from fastmcp import FastMCP
|
| 28 |
+
from fastmcp.contrib.mcp_mixin import MCPMixin, mcp_tool, mcp_resource, mcp_prompt
|
| 29 |
|
| 30 |
class MyComponent(MCPMixin):
|
| 31 |
@mcp_tool(name="my_tool", description="Does something cool.")
|
|
|
|
| 38 |
# This function can't be called by client because it's disabled
|
| 39 |
return "You'll never get here!"
|
| 40 |
|
| 41 |
+
# example of excluded parameter tool
|
| 42 |
+
@mcp_tool(
|
| 43 |
+
name="my_tool", description="Does something cool.",
|
| 44 |
+
enabled=False, exclude_args=['delete_everything'],
|
| 45 |
+
)
|
| 46 |
+
def excluded_param_tool_method(self, delete_everything=False):
|
| 47 |
+
# MCP tool calls can't pass the "delete_everything" argument
|
| 48 |
+
if delete_everything:
|
| 49 |
+
return "Nothing to delete, I bet you're not a tool :)"
|
| 50 |
+
return "You might be a tool if..."
|
| 51 |
+
|
| 52 |
+
# example tool w/annotations
|
| 53 |
@mcp_tool(
|
| 54 |
name="my_tool", description="Does something cool.",
|
| 55 |
+
annotations=ToolAnnotations(
|
| 56 |
+
title="Attn LLM, use this tool first!",
|
| 57 |
+
readOnlyHint=False,
|
| 58 |
+
destructiveHint=False,
|
| 59 |
+
idempotentHint=False,
|
| 60 |
+
)
|
| 61 |
)
|
| 62 |
def tool_method(self):
|
| 63 |
return "Tool executed!"
|
| 64 |
|
| 65 |
+
# example tool w/everything
|
| 66 |
+
@mcp_tool(
|
| 67 |
+
name="my_tool", description="Does something cool.",
|
| 68 |
+
enabled=True,
|
| 69 |
+
exclude_args=['delete_all'],
|
| 70 |
+
annotations=ToolAnnotations(
|
| 71 |
+
title="Attn LLM, use this tool first!",
|
| 72 |
+
readOnlyHint=False,
|
| 73 |
+
destructiveHint=False,
|
| 74 |
+
idempotentHint=False,
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
def tool_method(self, delete_all=False):
|
| 78 |
+
if delete_all:
|
| 79 |
+
return "99 records deleted. I bet you're not a tool :)"
|
| 80 |
+
return "Tool executed, but you might be a tool!"
|
| 81 |
+
|
| 82 |
@mcp_resource(uri="component://data")
|
| 83 |
def resource_method(self):
|
| 84 |
return {"data": "some data"}
|
|
|
|
| 88 |
def resource_method(self):
|
| 89 |
return {"data": "some data"}
|
| 90 |
|
| 91 |
+
# prompt
|
| 92 |
+
@mcp_prompt(name="A prompt")
|
| 93 |
+
def prompt_method(self, name):
|
| 94 |
+
return f"Whats up {name}?"
|
| 95 |
+
|
| 96 |
+
# disabled prompt
|
| 97 |
+
@mcp_prompt(name="A prompt", enabled=False)
|
| 98 |
+
def prompt_method(self, name):
|
| 99 |
+
return f"Whats up {name}?"
|
| 100 |
+
|
| 101 |
mcp_server = FastMCP()
|
| 102 |
component = MyComponent()
|
| 103 |
|