| 1 | # mcp_server.py DOX |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Own the `mcp_server.py` helper module. |
| 6 | - This module serves Agent Zero chats through a dynamic MCP proxy. |
| 7 | - Keep this file-level DOX profile synchronized with `mcp_server.py` because this directory is intentionally flat. |
| 8 | |
| 9 | ## Ownership |
| 10 | |
| 11 | - `mcp_server.py` owns the runtime implementation. |
| 12 | - `mcp_server.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation. |
| 13 | - Classes: |
| 14 | - `ToolResponse` (`BaseModel`) |
| 15 | - `ToolError` (`BaseModel`) |
| 16 | - `DynamicMcpProxy` (no explicit base class) |
| 17 | - `get_instance()` |
| 18 | - `reconfigure(self, token: str)` |
| 19 | - Top-level functions: |
| 20 | - `async send_message(message: Annotated[str, Field(description='The message to send to the remote Agent Zero Instance', title='message')], attachments: Annotated[list[str], Field(description='Optional: A list of attachments (file paths or web urls) to send to the remote Agent Zero Instance with the message. Default: Empty list', title='attachments')] | None=..., chat_id: Annotated[str, Field(description='Optional: ID of the chat. Used to continue a chat. This value is returned in response to sending previous message. Default: Empty string', title='chat_id')] | None=..., persistent_chat: Annotated[bool, Field(description='Optional: Whether to use a persistent chat. If true, the chat will be saved and can be continued later. Default: False.', title='persistent_chat')] | None=...) -> Annotated[Union[ToolResponse, ToolError], Field(description='The response from the remote Agent Zero Instance', title='response')]` |
| 21 | - `async finish_chat(chat_id: Annotated[str, Field(description='ID of the chat to be finished. This value is returned in response to sending previous message.', title='chat_id')]) -> Annotated[Union[ToolResponse, ToolError], Field(description='The response from the remote Agent Zero Instance', title='response')]` |
| 22 | - `async _run_chat(context: AgentContext, message: str, attachments: list[str] | None=...)` |
| 23 | - `async mcp_middleware(request: Request, call_next)`: Middleware to check if MCP server is enabled. |
| 24 | - Notable constants/configuration names: `_PRINTER`, `SEND_MESSAGE_DESCRIPTION`, `FINISH_CHAT_DESCRIPTION`. |
| 25 | |
| 26 | ## Runtime Contracts |
| 27 | |
| 28 | - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together. |
| 29 | - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change. |
| 30 | - Observed side-effect areas: filesystem reads, filesystem writes, filesystem deletion, network calls, settings/state persistence, secret handling, scheduler state. |
| 31 | - Imported dependency areas include: `agent`, `asyncio`, `contextvars`, `fastmcp`, `fastmcp.server.http`, `helpers`, `helpers.persist_chat`, `helpers.print_style`, `initialize`, `openai`, `os`, `pydantic`, `starlette.exceptions`, `starlette.middleware`, `starlette.middleware.base`, `starlette.requests`. |
| 32 | |
| 33 | ## Key Concepts |
| 34 | |
| 35 | - Important called helpers/classes observed in the source: `PrintStyle`, `contextvars.ContextVar`, `FastMCP`, `mcp_server.tool`, `Field`, `settings.get_settings`, `initialize_agent`, `AgentContext`, `ToolError`, `ToolResponse`, `context.reset`, `AgentContext.remove`, `remove_chat`, `context.communicate`, `threading.RLock`, `self.reconfigure`, `StreamableHTTPSessionManager`, `mcp_server._get_additional_http_routes`, `create_base_app`, `PrintStyle.error`. |
| 36 | - Keep request/response, tool, or helper semantics documented here at the same time as source changes. |
| 37 | |
| 38 | ## Work Guidance |
| 39 | |
| 40 | - Preserve public helper APIs used by core code and plugins unless every caller is updated. |
| 41 | - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded. |
| 42 | - Prefer adding cohesive helper functions here only when behavior is reused across modules. |
| 43 | |
| 44 | ## Verification |
| 45 | |
| 46 | - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers. |
| 47 | - Related tests observed by source search: |
| 48 | - `tests/test_default_prompt_budget.py` |
| 49 | - `tests/test_fasta2a_client.py` |
| 50 | - `tests/test_ws_security.py` |
| 51 | |
| 52 | ## Child DOX Index |
| 53 | |
| 54 | No child DOX files. |