pip install mcp openai
export OPENAI_API_KEY=sk-...
Create a simple MCP server that exposes file system tools.
1 #!/usr/bin/env python3 2 """Simple filesystem MCP server.""" 3 import asyncio 4 import os 5 from mcp.server.models import InitializationOptions 6 from mcp.server import NotificationOptions, Server 7 from mcp import types 8 import mcp.server.stdio 9 10 server = Server("filesystem-tools") 11 12 @server.list_tools() 13 async def handle_list_tools() -> list[types.Tool]: 14 return [ 15 types.Tool( 16 name="list_files", 17 description="List files in a directory", 18 inputSchema={"type": "object", "properties": {"directory": {"type": "string", "default": "."}}, "required": []} 19 ), 20 types.Tool( 21 name="read_file", 22 description="Read the contents of a text file", 23 inputSchema={"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]} 24 ), 25 ] 26 27 @server.call_tool() 28 async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]: 29 if name == "list_files": 30 directory = arguments.get("directory", ".") 31 files = os.listdir(directory) 32 return [types.TextContent(type="text", text=" 33 ".join(files))] 34 elif name == "read_file": 35 path = arguments["path"] 36 with open(path) as f: 37 return [types.TextContent(type="text", text=f.read())] 38 raise ValueError(f"Unknown tool: {name}") 39 40 async def main(): 41 async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): 42 await server.run(read_stream, write_stream, InitializationOptions( 43 server_name="filesystem-tools", server_version="0.1.0", 44 capabilities=server.get_capabilities(NotificationOptions(), {}) 45 )) 46 47 if __name__ == "__main__": 48 asyncio.run(main()) 49
MCP server runs and responds to mcp dev list-tools
Sign in to share your feedback and join the discussion.