mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 15:19:10 +02:00
docs: migrate example field reads to snake_case for MCP SDK v2
This commit is contained in:
parent
c6aa7f5e53
commit
f765ccb081
11 changed files with 24 additions and 24 deletions
|
|
@ -135,7 +135,7 @@ def greet(name: str) -> str:
|
|||
|
||||
async with Client(mcp) as client:
|
||||
# Initialization already happened automatically
|
||||
print(f"Server: {client.initialize_result.serverInfo.name}")
|
||||
print(f"Server: {client.initialize_result.server_info.name}")
|
||||
print(f"Instructions: {client.initialize_result.instructions}")
|
||||
print(f"Capabilities: {client.initialize_result.capabilities.tools}")
|
||||
```
|
||||
|
|
@ -154,7 +154,7 @@ async with client:
|
|||
|
||||
# Initialize manually with custom timeout
|
||||
result = await client.initialize(timeout=10.0)
|
||||
print(f"Server: {result.serverInfo.name}")
|
||||
print(f"Server: {result.server_info.name}")
|
||||
|
||||
# Now ready for operations
|
||||
tools = await client.list_tools()
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ The handler receives four parameters:
|
|||
</ResponseField>
|
||||
|
||||
<ResponseField name="params" type="ElicitRequestParams">
|
||||
The original MCP elicitation parameters, including the raw JSON schema in `params.requestedSchema`
|
||||
The original MCP elicitation parameters, including the raw JSON schema in `params.requested_schema`
|
||||
</ResponseField>
|
||||
|
||||
<ResponseField name="context" type="RequestContext">
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ async with client:
|
|||
for item in content:
|
||||
if hasattr(item, 'text'):
|
||||
print(f"Text content: {item.text}")
|
||||
print(f"MIME type: {item.mimeType}")
|
||||
print(f"MIME type: {item.mime_type}")
|
||||
```
|
||||
|
||||
Binary resources include images, PDFs, and other non-text data:
|
||||
|
|
@ -65,7 +65,7 @@ async with client:
|
|||
for item in content:
|
||||
if hasattr(item, 'blob'):
|
||||
print(f"Binary content: {len(item.blob)} bytes")
|
||||
print(f"MIME type: {item.mimeType}")
|
||||
print(f"MIME type: {item.mime_type}")
|
||||
|
||||
# Save to file
|
||||
with open("downloaded_logo.png", "wb") as f:
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ async def sampling_handler(
|
|||
conversation.append(f"{message.role}: {content}")
|
||||
|
||||
# Use the system prompt if provided
|
||||
system_prompt = params.systemPrompt or "You are a helpful assistant."
|
||||
system_prompt = params.system_prompt or "You are a helpful assistant."
|
||||
|
||||
# Integrate with your LLM service here
|
||||
return "Generated response based on the messages"
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ async with client:
|
|||
result = await client.call_tool_mcp("my_tool", {"param": "value"})
|
||||
# result -> fastmcp.types.CallToolResult
|
||||
|
||||
if result.isError:
|
||||
if result.is_error:
|
||||
print(f"Tool failed: {result.content}")
|
||||
else:
|
||||
print(f"Tool succeeded: {result.content}")
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ async def test_tool_schema_generation():
|
|||
return {"amount": amount, "tax": amount * rate, "total": amount * (1 + rate)}
|
||||
|
||||
tools = mcp.list_tools()
|
||||
schema = tools[0].inputSchema
|
||||
schema = tools[0].input_schema
|
||||
|
||||
# First run: snapshot() is empty, gets auto-populated
|
||||
# Subsequent runs: compares against stored snapshot
|
||||
|
|
|
|||
|
|
@ -1205,8 +1205,8 @@ When `list_page_size` is set, `tools/list`, `resources/list`, `resources/templat
|
|||
```python
|
||||
async with Client(server) as client:
|
||||
result = await client.list_tools_mcp()
|
||||
while result.nextCursor:
|
||||
result = await client.list_tools_mcp(cursor=result.nextCursor)
|
||||
while result.next_cursor:
|
||||
result = await client.list_tools_mcp(cursor=result.next_cursor)
|
||||
```
|
||||
|
||||
Documentation: [Pagination](/servers/pagination)
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ from fastmcp.types import Icon
|
|||
|
||||
icon = Icon(
|
||||
src="https://example.com/icon.png",
|
||||
mimeType="image/png",
|
||||
mime_type="image/png",
|
||||
sizes=["48x48"]
|
||||
)
|
||||
```
|
||||
|
|
@ -27,7 +27,7 @@ icon = Icon(
|
|||
The fields serve different purposes:
|
||||
|
||||
- **src**: URL or data URI pointing to the icon image
|
||||
- **mimeType** (optional): MIME type of the image (e.g., "image/png", "image/svg+xml")
|
||||
- **mime_type** (optional): MIME type of the image (e.g., "image/png", "image/svg+xml")
|
||||
- **sizes** (optional): Array of size descriptors (e.g., ["48x48"], ["any"])
|
||||
|
||||
## Server Icons
|
||||
|
|
@ -44,12 +44,12 @@ mcp = FastMCP(
|
|||
icons=[
|
||||
Icon(
|
||||
src="https://weather.example.com/icon-48.png",
|
||||
mimeType="image/png",
|
||||
mime_type="image/png",
|
||||
sizes=["48x48"]
|
||||
),
|
||||
Icon(
|
||||
src="https://weather.example.com/icon-96.png",
|
||||
mimeType="image/png",
|
||||
mime_type="image/png",
|
||||
sizes=["96x96"]
|
||||
),
|
||||
]
|
||||
|
|
@ -121,7 +121,7 @@ from fastmcp.utilities.types import Image
|
|||
# SVG icon as data URI
|
||||
svg_icon = Icon(
|
||||
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCI+PHBhdGggZD0iTTEyIDJDNi40OCAyIDIgNi40OCAyIDEyczQuNDggMTAgMTAgMTAgMTAtNC40OCAxMC0xMFMxNy41MiAyIDEyIDJ6Ii8+PC9zdmc+",
|
||||
mimeType="image/svg+xml"
|
||||
mime_type="image/svg+xml"
|
||||
)
|
||||
|
||||
@mcp.tool(icons=[svg_icon])
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ def analyze(data: str) -> dict:
|
|||
# ... many more tools, resources, prompts
|
||||
```
|
||||
|
||||
When `list_page_size` is configured, the `tools/list`, `resources/list`, `resources/templates/list`, and `prompts/list` endpoints all paginate their responses. Each response includes a `nextCursor` field when more results exist, which clients use to fetch subsequent pages.
|
||||
When `list_page_size` is configured, the `tools/list`, `resources/list`, `resources/templates/list`, and `prompts/list` endpoints all paginate their responses. Each response includes a `next_cursor` field when more results exist, which clients use to fetch subsequent pages.
|
||||
|
||||
### Cursor Format
|
||||
|
||||
|
|
@ -66,12 +66,12 @@ async with Client(server) as client:
|
|||
print(f"Page 1: {len(result.tools)} tools")
|
||||
|
||||
# Continue fetching while more pages exist
|
||||
while result.nextCursor:
|
||||
result = await client.list_tools_mcp(cursor=result.nextCursor)
|
||||
while result.next_cursor:
|
||||
result = await client.list_tools_mcp(cursor=result.next_cursor)
|
||||
print(f"Next page: {len(result.tools)} tools")
|
||||
```
|
||||
|
||||
The `_mcp` methods return the raw MCP protocol objects, which include both the items and the `nextCursor` for the next page. When `nextCursor` is `None`, you've reached the end of the result set.
|
||||
The `_mcp` methods return the raw MCP protocol objects, which include both the items and the `next_cursor` for the next page. When `next_cursor` is `None`, you've reached the end of the result set.
|
||||
|
||||
All four list operations support manual pagination:
|
||||
|
||||
|
|
|
|||
|
|
@ -443,7 +443,7 @@ async def research(question: str, ctx: Context) -> str:
|
|||
tool_results.append(
|
||||
ToolResultContent(
|
||||
type="tool_result",
|
||||
toolUseId=call.id,
|
||||
tool_use_id=call.id,
|
||||
content=[TextContent(type="text", text=result)],
|
||||
)
|
||||
)
|
||||
|
|
@ -452,14 +452,14 @@ async def research(question: str, ctx: Context) -> str:
|
|||
messages.append(SamplingMessage(role="user", content=tool_results))
|
||||
```
|
||||
|
||||
To report an error to the LLM, set `isError=True` on the tool result:
|
||||
To report an error to the LLM, set `is_error=True` on the tool result:
|
||||
|
||||
```python
|
||||
tool_result = ToolResultContent(
|
||||
type="tool_result",
|
||||
toolUseId=call.id,
|
||||
tool_use_id=call.id,
|
||||
content=[TextContent(type="text", text="Permission denied")],
|
||||
isError=True,
|
||||
is_error=True,
|
||||
)
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -746,7 +746,7 @@ ToolResult(content="Hello, world!")
|
|||
# List of content blocks
|
||||
ToolResult(content=[
|
||||
TextContent(type="text", text="Result: 42"),
|
||||
ImageContent(type="image", data="base64...", mimeType="image/png")
|
||||
ImageContent(type="image", data="base64...", mime_type="image/png")
|
||||
])
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue