Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
0d426dc
1
Parent(s): 72df1a6
Update docs
Browse files- docs/servers/openapi.mdx +41 -5
docs/servers/openapi.mdx
CHANGED
|
@@ -231,6 +231,38 @@ mcp = FastMCP.from_openapi(
|
|
| 231 |
|
| 232 |
## Customizing MCP Components
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
<VersionBadge version="2.5.0" />
|
| 235 |
|
| 236 |
By default, FastMCP creates MCP components using a variety of metadata from the OpenAPI spec, such as incorporating the OpenAPI description into the MCP component description.
|
|
@@ -271,7 +303,6 @@ mcp = FastMCP.from_openapi(
|
|
| 271 |
mcp_component_fn=customize_components,
|
| 272 |
)
|
| 273 |
```
|
| 274 |
-
|
| 275 |
## Request Parameter Handling
|
| 276 |
|
| 277 |
FastMCP intelligently handles different types of parameters in OpenAPI requests:
|
|
@@ -376,15 +407,15 @@ from fastmcp import FastMCP
|
|
| 376 |
# Your FastAPI app
|
| 377 |
app = FastAPI(title="My API", version="1.0.0")
|
| 378 |
|
| 379 |
-
@app.get("/items", tags=["items"])
|
| 380 |
def list_items():
|
| 381 |
return [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]
|
| 382 |
|
| 383 |
-
@app.get("/items/{item_id}", tags=["items", "detail"])
|
| 384 |
def get_item(item_id: int):
|
| 385 |
return {"id": item_id, "name": f"Item {item_id}"}
|
| 386 |
|
| 387 |
-
@app.post("/items", tags=["items", "create"])
|
| 388 |
def create_item(name: str):
|
| 389 |
return {"id": 3, "name": name}
|
| 390 |
|
|
@@ -395,6 +426,8 @@ if __name__ == "__main__":
|
|
| 395 |
mcp.run() # Run as MCP server
|
| 396 |
```
|
| 397 |
|
|
|
|
|
|
|
| 398 |
<Warning>
|
| 399 |
FastMCP servers are not FastAPI apps, even when created from one. To learn how to deploy them as an ASGI app, see the [ASGI Integration](/deployment/asgi) documentation.
|
| 400 |
</Warning>
|
|
@@ -413,6 +446,7 @@ mcp = FastMCP.from_fastapi(
|
|
| 413 |
app=app,
|
| 414 |
name="My Custom Server",
|
| 415 |
timeout=5.0,
|
|
|
|
| 416 |
route_maps=[
|
| 417 |
# Admin endpoints become tools
|
| 418 |
RouteMap(methods="*", pattern=r"^/admin/.*", mcp_type=MCPType.TOOL),
|
|
@@ -421,6 +455,9 @@ mcp = FastMCP.from_fastapi(
|
|
| 421 |
],
|
| 422 |
route_map_fn=my_route_mapper,
|
| 423 |
mcp_component_fn=my_component_customizer,
|
|
|
|
|
|
|
|
|
|
| 424 |
)
|
| 425 |
```
|
| 426 |
|
|
@@ -430,4 +467,3 @@ mcp = FastMCP.from_fastapi(
|
|
| 430 |
- **Schema inheritance**: Pydantic models and validation are preserved
|
| 431 |
- **ASGI transport**: Direct in-memory communication (no HTTP overhead)
|
| 432 |
- **Full FastAPI features**: Dependencies, middleware, authentication all work
|
| 433 |
-
|
|
|
|
| 231 |
|
| 232 |
## Customizing MCP Components
|
| 233 |
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
### Component Names
|
| 237 |
+
|
| 238 |
+
<VersionBadge version="2.5.0" />
|
| 239 |
+
|
| 240 |
+
FastMCP automatically generates names for MCP components based on the OpenAPI specification. By default, it uses the `operationId` from your OpenAPI spec, up to the first double underscore (`__`).
|
| 241 |
+
|
| 242 |
+
All component names are automatically:
|
| 243 |
+
- **Slugified**: Spaces and special characters are converted to underscores or removed
|
| 244 |
+
- **Truncated**: Limited to 48 characters maximum to ensure compatibility
|
| 245 |
+
- **Unique**: If multiple components have the same name, a number is automatically appended to make them unique
|
| 246 |
+
|
| 247 |
+
For more control over component names, you can provide an `mcp_names` dictionary that maps `operationId` values to your desired names. The `operationId` must be exactly as it appears in the OpenAPI spec. The provided name will always be slugified and truncated.
|
| 248 |
+
|
| 249 |
+
```python {5-9}
|
| 250 |
+
from fastmcp import FastMCP
|
| 251 |
+
|
| 252 |
+
mcp = FastMCP.from_openapi(
|
| 253 |
+
...
|
| 254 |
+
mcp_names={
|
| 255 |
+
"list_users__with_pagination": "user_list",
|
| 256 |
+
"create_user__admin_required": "create_user",
|
| 257 |
+
"get_user_details__admin_required": "user_detail",
|
| 258 |
+
}
|
| 259 |
+
)
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
Any `operationId` not found in `mcp_names` will use the default strategy (operationId up to the first `__`).
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
### Advanced Customization
|
| 266 |
<VersionBadge version="2.5.0" />
|
| 267 |
|
| 268 |
By default, FastMCP creates MCP components using a variety of metadata from the OpenAPI spec, such as incorporating the OpenAPI description into the MCP component description.
|
|
|
|
| 303 |
mcp_component_fn=customize_components,
|
| 304 |
)
|
| 305 |
```
|
|
|
|
| 306 |
## Request Parameter Handling
|
| 307 |
|
| 308 |
FastMCP intelligently handles different types of parameters in OpenAPI requests:
|
|
|
|
| 407 |
# Your FastAPI app
|
| 408 |
app = FastAPI(title="My API", version="1.0.0")
|
| 409 |
|
| 410 |
+
@app.get("/items", tags=["items"], operation_id="list_items")
|
| 411 |
def list_items():
|
| 412 |
return [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]
|
| 413 |
|
| 414 |
+
@app.get("/items/{item_id}", tags=["items", "detail"], operation_id="get_item")
|
| 415 |
def get_item(item_id: int):
|
| 416 |
return {"id": item_id, "name": f"Item {item_id}"}
|
| 417 |
|
| 418 |
+
@app.post("/items", tags=["items", "create"], operation_id="create_item")
|
| 419 |
def create_item(name: str):
|
| 420 |
return {"id": 3, "name": name}
|
| 421 |
|
|
|
|
| 426 |
mcp.run() # Run as MCP server
|
| 427 |
```
|
| 428 |
|
| 429 |
+
Note that operation ids are optional, but are used to create component names. You can also provide custom names, just like with OpenAPI specs.
|
| 430 |
+
|
| 431 |
<Warning>
|
| 432 |
FastMCP servers are not FastAPI apps, even when created from one. To learn how to deploy them as an ASGI app, see the [ASGI Integration](/deployment/asgi) documentation.
|
| 433 |
</Warning>
|
|
|
|
| 446 |
app=app,
|
| 447 |
name="My Custom Server",
|
| 448 |
timeout=5.0,
|
| 449 |
+
mcp_names={"operationId": "friendly_name"}, # Custom component names
|
| 450 |
route_maps=[
|
| 451 |
# Admin endpoints become tools
|
| 452 |
RouteMap(methods="*", pattern=r"^/admin/.*", mcp_type=MCPType.TOOL),
|
|
|
|
| 455 |
],
|
| 456 |
route_map_fn=my_route_mapper,
|
| 457 |
mcp_component_fn=my_component_customizer,
|
| 458 |
+
mcp_names={
|
| 459 |
+
"get_user_details_users__user_id__get": "get_user_details",
|
| 460 |
+
}
|
| 461 |
)
|
| 462 |
```
|
| 463 |
|
|
|
|
| 467 |
- **Schema inheritance**: Pydantic models and validation are preserved
|
| 468 |
- **ASGI transport**: Direct in-memory communication (no HTTP overhead)
|
| 469 |
- **Full FastAPI features**: Dependencies, middleware, authentication all work
|
|
|