test_mcp.py•1.45 kB
#!/usr/bin/env python3
"""
Test MCP Server
"""
import asyncio
import json
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
# Create server instance
server = Server("test-mcp-server")
@server.list_tools()
async def list_tools() -> list[Tool]:
"""List available tools"""
return [
Tool(
name="echo",
description="Echo back the input text",
inputSchema={
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Text to echo back"
}
},
"required": ["text"]
}
)
]
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
"""Handle tool calls"""
if name == "echo":
text = arguments.get("text", "")
return [TextContent(type="text", text=f"Echo: {text}")]
else:
raise ValueError(f"Unknown tool: {name}")
async def main():
"""Main function"""
print("Starting MCP server...", file=sys.stderr)
async with stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options()
)
if __name__ == "__main__":
import sys
asyncio.run(main())