mcp.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """``hermes mcp`` subcommand parser."""
  2. from __future__ import annotations
  3. import argparse
  4. from typing import Callable
  5. def build_mcp_parser(subparsers, *, cmd_mcp: Callable) -> None:
  6. """Attach the ``mcp`` subcommand to ``subparsers``."""
  7. mcp_parser = subparsers.add_parser(
  8. "mcp", help="Manage MCP server connections",
  9. description="Manage MCP server connections.\n\n"
  10. "MCP servers provide additional tools via the Model Context Protocol.\n"
  11. "Use 'hermes mcp add' to connect to a new server. Running Hermes itself as an\n"
  12. "MCP server is the mcp-serve plugin: `hermes plugins install mcp-serve`.")
  13. mcp_sub = mcp_parser.add_subparsers(dest="mcp_action")
  14. mcp_add_p = mcp_sub.add_parser("add", help="Add an MCP server (discovery-first install)")
  15. mcp_add_p.add_argument("name", help="Server name (used as config key)")
  16. mcp_add_p.add_argument("--url", help="HTTP/SSE endpoint URL")
  17. # dest="mcp_command": the default dest="command" would clobber the top-level
  18. # subcommand name the dispatcher routes on (None when omitted → falls into chat).
  19. mcp_add_p.add_argument("--command", dest="mcp_command", help="Stdio command (e.g. npx)")
  20. mcp_add_p.add_argument(
  21. "--args", nargs=argparse.REMAINDER, default=[],
  22. help="Arguments for stdio command; must be the last option")
  23. mcp_add_p.add_argument("--auth", choices=["oauth", "header"], help="Auth method")
  24. mcp_add_p.add_argument("--preset", help="Known MCP preset name")
  25. mcp_add_p.add_argument(
  26. "--connect-timeout", type=float,
  27. help="Timeout in seconds for initial connection and tool discovery")
  28. mcp_add_p.add_argument(
  29. "--env", nargs="*", default=[], help="Environment variables for stdio servers (KEY=VALUE)")
  30. mcp_rm_p = mcp_sub.add_parser("remove", aliases=["rm"], help="Remove an MCP server")
  31. mcp_rm_p.add_argument("name", help="Server name to remove")
  32. mcp_sub.add_parser("list", aliases=["ls"], help="List configured MCP servers")
  33. mcp_test_p = mcp_sub.add_parser("test", help="Test MCP server connection")
  34. mcp_test_p.add_argument("name", help="Server name to test")
  35. mcp_cfg_p = mcp_sub.add_parser("configure", aliases=["config"], help="Toggle tool selection")
  36. mcp_cfg_p.add_argument("name", help="Server name to configure")
  37. mcp_login_p = mcp_sub.add_parser(
  38. "login", help="Force re-authentication for an OAuth-based MCP server")
  39. mcp_login_p.add_argument("name", help="Server name to re-authenticate")
  40. mcp_login_p.add_argument(
  41. "--flow", choices=["browser", "device"], default=None,
  42. help="OAuth flow (overrides oauth.flow): browser PKCE or RFC 8628 device code")
  43. mcp_reauth_p = mcp_sub.add_parser(
  44. "reauth", help="Re-authenticate one OAuth MCP server, or all of them (--all)")
  45. mcp_reauth_p.add_argument(
  46. "name", nargs="?", help="Server name to re-authenticate (omit with --all)")
  47. mcp_reauth_p.add_argument(
  48. "--all", action="store_true",
  49. help="Re-authenticate every OAuth server in config, one at a time")
  50. # Catalog (Nous-approved MCPs shipped with the repo)
  51. mcp_sub.add_parser(
  52. "picker", help="Interactive catalog picker (also the default for `hermes mcp`)")
  53. mcp_sub.add_parser("catalog", help="List Nous-approved MCPs available for one-click install")
  54. mcp_install_p = mcp_sub.add_parser(
  55. "install", help="Install a catalog MCP by name (e.g. `hermes mcp install deepwiki`)")
  56. mcp_install_p.add_argument("identifier", help="Catalog entry name (or `official/<name>`)")
  57. mcp_parser.set_defaults(func=cmd_mcp)