Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Update examples

Jeremiah Lowinf4d23be14565
diff --git a/docs/patterns/fastapi.mdx b/docs/patterns/fastapi.mdx
index 78591a1e0..681a4456b 100644
--- a/docs/patterns/fastapi.mdx
+++ b/docs/patterns/fastapi.mdx
@@ -73,7 +73,7 @@ For more details on route mapping or custom mapping rules, see the [OpenAPI inte
Here's a more detailed example with a data model:
-```python
+```python [expandable]
import asyncio
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
@@ -108,24 +108,32 @@ def create_item(item: Item):
return items[item_id]
# Test your MCP server with a client
-async def test():
- # Create MCP server from FastAPI app
- mcp = await FastMCP.from_fastapi(app=app)
-
+async def check_mcp(mcp: FastMCP):
# List the components that were created
- tools = await mcp.list_tools()
- resources = await mcp.list_resources()
- templates = await mcp.list_resource_templates()
+ tools = await mcp.get_tools()
+ resources = await mcp.get_resources()
+ templates = await mcp.get_resource_templates()
- print(f"Generated {len(tools)} tools")
- print(f"Generated {len(resources)} resources")
- print(f"Generated {len(templates)} templates")
+ print(
+ f"{len(tools)} Tool(s): {', '.join([t.name for t in tools.values()])}"
+ )
+ print(
+ f"{len(resources)} Resource(s): {', '.join([r.name for r in resources.values()])}"
+ )
+ print(
+ f"{len(templates)} Resource Template(s): {', '.join([t.name for t in templates.values()])}"
+ )
- # In a real scenario, you would run the server:
- # mcp.run()
+ return mcp
if __name__ == "__main__":
- asyncio.run(test())
+ # Create MCP server from FastAPI app
+ mcp = FastMCP.from_fastapi(app=app)
+
+ asyncio.run(check_mcp(mcp))
+
+ # In a real scenario, you would run the server:
+ mcp.run()
```
## Benefits
diff --git a/docs/patterns/openapi.mdx b/docs/patterns/openapi.mdx
index 1eb6835d0..4d37e9cdb 100644
--- a/docs/patterns/openapi.mdx
+++ b/docs/patterns/openapi.mdx
@@ -108,24 +108,33 @@ mcp = await FastMCP.from_openapi(
## Complete Example
-```python
+```python [expandable]
import asyncio
+
import httpx
+
from fastmcp import FastMCP
# Sample OpenAPI spec for a Pet Store API
petstore_spec = {
"openapi": "3.0.0",
+ "info": {
+ "title": "Pet Store API",
+ "version": "1.0.0",
+ "description": "A sample API for managing pets",
+ },
"paths": {
"/pets": {
"get": {
"operationId": "listPets",
- "summary": "List all pets"
+ "summary": "List all pets",
+ "responses": {"200": {"description": "A list of pets"}},
},
"post": {
"operationId": "createPet",
- "summary": "Create a new pet"
- }
+ "summary": "Create a new pet",
+ "responses": {"201": {"description": "Pet created successfully"}},
+ },
},
"/pets/{petId}": {
"get": {
@@ -136,38 +145,50 @@ petstore_spec = {
"name": "petId",
"in": "path",
"required": True,
- "schema": {"type": "string"}
+ "schema": {"type": "string"},
}
- ]
+ ],
+ "responses": {
+ "200": {"description": "Pet details"},
+ "404": {"description": "Pet not found"},
+ },
}
- }
- }
+ },
+ },
}
-async def main():
+
+async def check_mcp(mcp: FastMCP):
+ # List what components were created
+ tools = await mcp.get_tools()
+ resources = await mcp.get_resources()
+ templates = await mcp.get_resource_templates()
+
+ print(
+ f"{len(tools)} Tool(s): {', '.join([t.name for t in tools.values()])}"
+ ) # Should include createPet
+ print(
+ f"{len(resources)} Resource(s): {', '.join([r.name for r in resources.values()])}"
+ ) # Should include listPets
+ print(
+ f"{len(templates)} Resource Template(s): {', '.join([t.name for t in templates.values()])}"
+ ) # Should include getPet
+
+ return mcp
+
+
+if __name__ == "__main__":
# Client for the Pet Store API
client = httpx.AsyncClient(base_url="https://petstore.example.com/api")
-
+
# Create the MCP server
- mcp = await FastMCP.from_openapi(
- openapi_spec=petstore_spec,
- client=client,
- name="PetStore"
+ mcp = FastMCP.from_openapi(
+ openapi_spec=petstore_spec, client=client, name="PetStore"
)
-
- # List what components were created
- tools = await mcp.list_tools()
- resources = await mcp.list_resources()
- templates = await mcp.list_resource_templates()
-
- print(f"Tools: {len(tools)}") # Should include createPet
- print(f"Resources: {len(resources)}") # Should include listPets
- print(f"Templates: {len(templates)}") # Should include getPet
-
+
+ asyncio.run(check_mcp(mcp))
+
# Start the MCP server
mcp.run()
-
-if __name__ == "__main__":
- asyncio.run(main())
```