Jeremiah Lowin commited on
Commit
735378c
·
1 Parent(s): 36d15c2

Update examples

Browse files
Files changed (2) hide show
  1. docs/patterns/fastapi.mdx +22 -14
  2. docs/patterns/openapi.mdx +48 -27
docs/patterns/fastapi.mdx CHANGED
@@ -73,7 +73,7 @@ For more details on route mapping or custom mapping rules, see the [OpenAPI inte
73
 
74
  Here's a more detailed example with a data model:
75
 
76
- ```python
77
  import asyncio
78
  from fastapi import FastAPI, HTTPException
79
  from pydantic import BaseModel
@@ -108,24 +108,32 @@ def create_item(item: Item):
108
  return items[item_id]
109
 
110
  # Test your MCP server with a client
111
- async def test():
112
- # Create MCP server from FastAPI app
113
- mcp = await FastMCP.from_fastapi(app=app)
114
-
115
  # List the components that were created
116
- tools = await mcp.list_tools()
117
- resources = await mcp.list_resources()
118
- templates = await mcp.list_resource_templates()
119
 
120
- print(f"Generated {len(tools)} tools")
121
- print(f"Generated {len(resources)} resources")
122
- print(f"Generated {len(templates)} templates")
 
 
 
 
 
 
123
 
124
- # In a real scenario, you would run the server:
125
- # mcp.run()
126
 
127
  if __name__ == "__main__":
128
- asyncio.run(test())
 
 
 
 
 
 
129
  ```
130
 
131
  ## Benefits
 
73
 
74
  Here's a more detailed example with a data model:
75
 
76
+ ```python [expandable]
77
  import asyncio
78
  from fastapi import FastAPI, HTTPException
79
  from pydantic import BaseModel
 
108
  return items[item_id]
109
 
110
  # Test your MCP server with a client
111
+ async def check_mcp(mcp: FastMCP):
 
 
 
112
  # List the components that were created
113
+ tools = await mcp.get_tools()
114
+ resources = await mcp.get_resources()
115
+ templates = await mcp.get_resource_templates()
116
 
117
+ print(
118
+ f"{len(tools)} Tool(s): {', '.join([t.name for t in tools.values()])}"
119
+ )
120
+ print(
121
+ f"{len(resources)} Resource(s): {', '.join([r.name for r in resources.values()])}"
122
+ )
123
+ print(
124
+ f"{len(templates)} Resource Template(s): {', '.join([t.name for t in templates.values()])}"
125
+ )
126
 
127
+ return mcp
 
128
 
129
  if __name__ == "__main__":
130
+ # Create MCP server from FastAPI app
131
+ mcp = FastMCP.from_fastapi(app=app)
132
+
133
+ asyncio.run(check_mcp(mcp))
134
+
135
+ # In a real scenario, you would run the server:
136
+ mcp.run()
137
  ```
138
 
139
  ## Benefits
docs/patterns/openapi.mdx CHANGED
@@ -108,24 +108,33 @@ mcp = await FastMCP.from_openapi(
108
 
109
  ## Complete Example
110
 
111
- ```python
112
  import asyncio
 
113
  import httpx
 
114
  from fastmcp import FastMCP
115
 
116
  # Sample OpenAPI spec for a Pet Store API
117
  petstore_spec = {
118
  "openapi": "3.0.0",
 
 
 
 
 
119
  "paths": {
120
  "/pets": {
121
  "get": {
122
  "operationId": "listPets",
123
- "summary": "List all pets"
 
124
  },
125
  "post": {
126
  "operationId": "createPet",
127
- "summary": "Create a new pet"
128
- }
 
129
  },
130
  "/pets/{petId}": {
131
  "get": {
@@ -136,38 +145,50 @@ petstore_spec = {
136
  "name": "petId",
137
  "in": "path",
138
  "required": True,
139
- "schema": {"type": "string"}
140
  }
141
- ]
 
 
 
 
142
  }
143
- }
144
- }
145
  }
146
 
147
- async def main():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  # Client for the Pet Store API
149
  client = httpx.AsyncClient(base_url="https://petstore.example.com/api")
150
-
151
  # Create the MCP server
152
- mcp = await FastMCP.from_openapi(
153
- openapi_spec=petstore_spec,
154
- client=client,
155
- name="PetStore"
156
  )
157
-
158
- # List what components were created
159
- tools = await mcp.list_tools()
160
- resources = await mcp.list_resources()
161
- templates = await mcp.list_resource_templates()
162
-
163
- print(f"Tools: {len(tools)}") # Should include createPet
164
- print(f"Resources: {len(resources)}") # Should include listPets
165
- print(f"Templates: {len(templates)}") # Should include getPet
166
-
167
  # Start the MCP server
168
  mcp.run()
169
-
170
- if __name__ == "__main__":
171
- asyncio.run(main())
172
  ```
173
 
 
108
 
109
  ## Complete Example
110
 
111
+ ```python [expandable]
112
  import asyncio
113
+
114
  import httpx
115
+
116
  from fastmcp import FastMCP
117
 
118
  # Sample OpenAPI spec for a Pet Store API
119
  petstore_spec = {
120
  "openapi": "3.0.0",
121
+ "info": {
122
+ "title": "Pet Store API",
123
+ "version": "1.0.0",
124
+ "description": "A sample API for managing pets",
125
+ },
126
  "paths": {
127
  "/pets": {
128
  "get": {
129
  "operationId": "listPets",
130
+ "summary": "List all pets",
131
+ "responses": {"200": {"description": "A list of pets"}},
132
  },
133
  "post": {
134
  "operationId": "createPet",
135
+ "summary": "Create a new pet",
136
+ "responses": {"201": {"description": "Pet created successfully"}},
137
+ },
138
  },
139
  "/pets/{petId}": {
140
  "get": {
 
145
  "name": "petId",
146
  "in": "path",
147
  "required": True,
148
+ "schema": {"type": "string"},
149
  }
150
+ ],
151
+ "responses": {
152
+ "200": {"description": "Pet details"},
153
+ "404": {"description": "Pet not found"},
154
+ },
155
  }
156
+ },
157
+ },
158
  }
159
 
160
+
161
+ async def check_mcp(mcp: FastMCP):
162
+ # List what components were created
163
+ tools = await mcp.get_tools()
164
+ resources = await mcp.get_resources()
165
+ templates = await mcp.get_resource_templates()
166
+
167
+ print(
168
+ f"{len(tools)} Tool(s): {', '.join([t.name for t in tools.values()])}"
169
+ ) # Should include createPet
170
+ print(
171
+ f"{len(resources)} Resource(s): {', '.join([r.name for r in resources.values()])}"
172
+ ) # Should include listPets
173
+ print(
174
+ f"{len(templates)} Resource Template(s): {', '.join([t.name for t in templates.values()])}"
175
+ ) # Should include getPet
176
+
177
+ return mcp
178
+
179
+
180
+ if __name__ == "__main__":
181
  # Client for the Pet Store API
182
  client = httpx.AsyncClient(base_url="https://petstore.example.com/api")
183
+
184
  # Create the MCP server
185
+ mcp = FastMCP.from_openapi(
186
+ openapi_spec=petstore_spec, client=client, name="PetStore"
 
 
187
  )
188
+
189
+ asyncio.run(check_mcp(mcp))
190
+
 
 
 
 
 
 
 
191
  # Start the MCP server
192
  mcp.run()
 
 
 
193
  ```
194