Skip to content

Commit 15cd7df

Browse files
committed
refactor: Update FastMCP examples to use new import path
Update all FastMCP examples to use mcp.server.fastmcp instead of fastmcp. Add tests to verify example servers work correctly. Changes: - Update import paths in all example files - Create test_examples.py to verify functionality - Fix test assertions to match actual response format
1 parent fe75f43 commit 15cd7df

File tree

9 files changed

+613
-0
lines changed

9 files changed

+613
-0
lines changed

examples/fastmcp/complex_inputs.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
FastMCP Complex inputs Example
3+
4+
Demonstrates validation via pydantic with complex models.
5+
"""
6+
7+
from pydantic import BaseModel, Field
8+
from typing import Annotated
9+
from mcp.server.fastmcp import FastMCP
10+
11+
mcp = FastMCP("Shrimp Tank")
12+
13+
14+
class ShrimpTank(BaseModel):
15+
class Shrimp(BaseModel):
16+
name: Annotated[str, Field(max_length=10)]
17+
18+
shrimp: list[Shrimp]
19+
20+
21+
@mcp.tool()
22+
def name_shrimp(
23+
tank: ShrimpTank,
24+
# You can use pydantic Field in function signatures for validation.
25+
extra_names: Annotated[list[str], Field(max_length=10)],
26+
) -> list[str]:
27+
"""List all shrimp names in the tank"""
28+
return [shrimp.name for shrimp in tank.shrimp] + extra_names

examples/fastmcp/desktop.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
FastMCP Desktop Example
3+
4+
A simple example that exposes the desktop directory as a resource.
5+
"""
6+
7+
from pathlib import Path
8+
9+
from mcp.server.fastmcp import FastMCP
10+
11+
# Create server
12+
mcp = FastMCP("Demo")
13+
14+
15+
@mcp.resource("dir://desktop")
16+
def desktop() -> list[str]:
17+
"""List the files in the user's desktop"""
18+
desktop = Path.home() / "Desktop"
19+
return [str(f) for f in desktop.iterdir()]
20+
21+
22+
@mcp.tool()
23+
def add(a: int, b: int) -> int:
24+
"""Add two numbers"""
25+
return a + b

examples/fastmcp/echo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
FastMCP Echo Server
3+
"""
4+
5+
from mcp.server.fastmcp import FastMCP
6+
7+
# Create server
8+
mcp = FastMCP("Echo Server")
9+
10+
11+
@mcp.tool()
12+
def echo_tool(text: str) -> str:
13+
"""Echo the input text"""
14+
return text
15+
16+
17+
@mcp.resource("echo://static")
18+
def echo_resource() -> str:
19+
return "Echo!"
20+
21+
22+
@mcp.resource("echo://{text}")
23+
def echo_template(text: str) -> str:
24+
"""Echo the input text"""
25+
return f"Echo: {text}"
26+
27+
28+
@mcp.prompt("echo")
29+
def echo_prompt(text: str) -> str:
30+
return text

0 commit comments

Comments
 (0)