sse_mcp_server.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import os
  2. from dotenv import load_dotenv
  3. from pydantic import Field
  4. from agency_swarm import BaseTool, function_tool, run_mcp
  5. load_dotenv()
  6. # v0.X BaseTool-style tool example (equally supported)
  7. class GetSecretWordTool(BaseTool):
  8. seed: int = Field(..., description="The seed for the random number generator")
  9. def run(self) -> str:
  10. """Returns a secret word based on the seed"""
  11. return "Strawberry" if self.seed % 2 == 0 else "Apple"
  12. @function_tool
  13. async def list_directory() -> str:
  14. """Returns the contents of the current directory"""
  15. import os
  16. dir_path = os.path.dirname(os.path.abspath(__file__))
  17. return os.listdir(dir_path)
  18. if __name__ == "__main__":
  19. if not os.getenv("APP_TOKEN") or os.getenv("APP_TOKEN") == "":
  20. os.environ["APP_TOKEN"] = "test_token_123"
  21. print("APP_TOKEN not set, using default token: test_token_123")
  22. run_mcp(
  23. tools=[GetSecretWordTool, list_directory],
  24. transport="sse",
  25. # FastMCP defaults this to 0, which reliably produces a scary shutdown error log.
  26. uvicorn_config={"timeout_graceful_shutdown": 1},
  27. )