-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add schema validation to lowlevel server #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,155
−8
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0cbc9e
Add inputSchema validation to lowlevel server
bhosmer-ant 35b7efb
Add outputSchema validation to lowlevel server
bhosmer-ant 3c30ba4
update README.md and add example
bhosmer-ant e69fd78
address comments
bhosmer-ant 193b28f
add validate_input flag to @server.call_tool
bhosmer-ant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Example low-level MCP server demonstrating structured output support. | ||
This example shows how to use the low-level server API to return | ||
structured data from tools, with automatic validation against output | ||
schemas. | ||
""" | ||
|
||
import asyncio | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
import mcp.server.stdio | ||
import mcp.types as types | ||
from mcp.server.lowlevel import NotificationOptions, Server | ||
from mcp.server.models import InitializationOptions | ||
|
||
# Create low-level server instance | ||
server = Server("structured-output-lowlevel-example") | ||
|
||
|
||
@server.list_tools() | ||
async def list_tools() -> list[types.Tool]: | ||
"""List available tools with their schemas.""" | ||
return [ | ||
types.Tool( | ||
name="get_weather", | ||
description="Get weather information (simulated)", | ||
inputSchema={ | ||
"type": "object", | ||
"properties": {"city": {"type": "string", "description": "City name"}}, | ||
"required": ["city"], | ||
}, | ||
outputSchema={ | ||
"type": "object", | ||
"properties": { | ||
"temperature": {"type": "number"}, | ||
"conditions": {"type": "string"}, | ||
"humidity": {"type": "integer", "minimum": 0, "maximum": 100}, | ||
"wind_speed": {"type": "number"}, | ||
"timestamp": {"type": "string", "format": "date-time"}, | ||
}, | ||
"required": ["temperature", "conditions", "humidity", "wind_speed", "timestamp"], | ||
}, | ||
), | ||
] | ||
|
||
|
||
@server.call_tool() | ||
async def call_tool(name: str, arguments: dict[str, Any]) -> Any: | ||
""" | ||
Handle tool call with structured output. | ||
""" | ||
|
||
if name == "get_weather": | ||
# city = arguments["city"] # Would be used with real weather API | ||
|
||
# Simulate weather data (in production, call a real weather API) | ||
import random | ||
|
||
weather_conditions = ["sunny", "cloudy", "rainy", "partly cloudy", "foggy"] | ||
|
||
weather_data = { | ||
"temperature": round(random.uniform(0, 35), 1), | ||
"conditions": random.choice(weather_conditions), | ||
"humidity": random.randint(30, 90), | ||
"wind_speed": round(random.uniform(0, 30), 1), | ||
"timestamp": datetime.now().isoformat(), | ||
} | ||
|
||
# Return structured data only | ||
# The low-level server will serialize this to JSON content automatically | ||
return weather_data | ||
|
||
else: | ||
raise ValueError(f"Unknown tool: {name}") | ||
|
||
|
||
async def run(): | ||
"""Run the low-level server using stdio transport.""" | ||
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): | ||
await server.run( | ||
read_stream, | ||
write_stream, | ||
InitializationOptions( | ||
server_name="structured-output-lowlevel-example", | ||
server_version="0.1.0", | ||
capabilities=server.get_capabilities( | ||
notification_options=NotificationOptions(), | ||
experimental_capabilities={}, | ||
), | ||
), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(run()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.