-
-
Save ochafik/29a1989e2bbe8619e072fd06dd0c3376 to your computer and use it in GitHub Desktop.
mini MCP server
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
| #!/usr/bin/env uv run | |
| # /// script | |
| # requires-python = ">=3.11" | |
| # dependencies = [ | |
| # "mcp", | |
| # ] | |
| # /// | |
| ''' | |
| claude mcp add mini $PWD/mini.py | |
| claude --allowedTools mcp__mini__add -p "add numbers 2 & 3, 4 & 5, 6 & 7 with the mini mcp" | |
| cat mini_output.txt | |
| # [pid: 51155] Starting mini MCP server | |
| # [pid: 51155] 2 + 3 = 5 | |
| # [pid: 51155] 4 + 5 = 9 | |
| # [pid: 51155] 6 + 7 = 13 | |
| claude | |
| # add numbers 2 & 3, 4 & 5, 6 & 7 with the mini mcp | |
| cat mini_output.txt | |
| # [pid: 54051] Starting mini MCP server | |
| # [pid: 54839] Starting mini MCP server | |
| # [pid: 54841] Starting mini MCP server | |
| # [pid: 54841] 2 + 3 = 5 | |
| # [pid: 54862] Starting mini MCP server | |
| # [pid: 54934] Starting mini MCP server | |
| # [pid: 54934] 4 + 5 = 9 | |
| # [pid: 54956] Starting mini MCP server | |
| # [pid: 54958] Starting mini MCP server | |
| # [pid: 54958] 6 + 7 = 13 | |
| ''' | |
| import fcntl | |
| import sys | |
| import os | |
| from mcp.server.fastmcp import FastMCP | |
| mcp = FastMCP("Demo") | |
| output_file = "mini_output.txt" | |
| def log(message: str) -> None: | |
| with open(output_file, "a") as f: | |
| fcntl.flock(f.fileno(), fcntl.LOCK_EX) | |
| try: | |
| f.write(f"[pid: {os.getpid()}] {message}\n") | |
| finally: | |
| fcntl.flock(f.fileno(), fcntl.LOCK_UN) | |
| @mcp.tool() | |
| def add(a: int, b: int) -> int: | |
| """Add two numbers""" | |
| res = a + b | |
| log(f"{a} + {b} = {res}") | |
| return res | |
| if __name__ == "__main__": | |
| print(f"Output will be written to {output_file}", file=sys.stderr) # noqa: T201 | |
| log("Starting mini MCP server") | |
| mcp.run(transport="stdio") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment