stdio_mcp_server.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from pydantic import BaseModel, Field
  2. from agency_swarm import function_tool, run_mcp
  3. class TimeArgs(BaseModel):
  4. time_zone: str = Field(..., description="The time zone to get the current time for used in")
  5. time_format: str | None = Field(None, description="The format of the time to return")
  6. # Define the async function that implements the tool logic using the decorator
  7. @function_tool
  8. async def get_unique_id() -> str:
  9. """Returns a unique id"""
  10. return "Unique ID: 12332211"
  11. @function_tool
  12. async def get_current_time(args: TimeArgs) -> str:
  13. """Returns the current time using datetime library"""
  14. import datetime
  15. from zoneinfo import ZoneInfo
  16. try:
  17. # Use the specified timezone
  18. tz = ZoneInfo(args.time_zone)
  19. current_time = datetime.datetime.now(tz)
  20. # Use custom format if provided, otherwise use default
  21. if args.time_format:
  22. formatted_time = current_time.strftime(args.time_format)
  23. else:
  24. formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S %Z")
  25. return f"Current time in {args.time_zone}: {formatted_time}"
  26. except Exception as e:
  27. return f"Error getting time for timezone {args.time_zone}: {str(e)}"
  28. if __name__ == "__main__":
  29. run_mcp(tools=[get_unique_id, get_current_time], transport="stdio")