Purpose
-------
MCP server core (Layers 1 + orchestration) for the ``oct-mcp`` server.
Creates a :class:`mcp.server.fastmcp.FastMCP` instance, binds it to a
validated project root (Layer 1 — Gateway), and exposes the nine Phase
5A read-only tools via :mod:`oct.mcp.tools`.
Responsibilities
----------------
- Implement :func:`start_server`: resolve + validate project root,
load config, instantiate all pipeline layers, register tools, run.
- Implement the token-bucket rate limiter (Layer 1) via
:class:`RateLimiter`.
- Expose ``_server_state`` for tools to access the shared pipeline
instances (singleton pattern: one server = one context).
Diagnostics
-----------
Domain: MCP
Levels:
L1 — errors: startup failures, workspace binding failures
L2 — lifecycle: server start/stop, workspace bound
L3 — request details
L4 — deep trace
Contracts
---------
- :func:`start_server` raises ``SystemExit(1)`` on fatal startup errors
(invalid project root, no ``oct`` command) so ``__main__.py`` can
propagate a clean exit code.
- The rate limiter is per-server-instance (not per-session), which is
sufficient for the stdio Phase 5A transport where one server process
handles one client session.
- ``_server_state`` is set before :meth:`FastMCP.run` is called and is
available to all tool handlers during their execution.
| None oct.mcp.server.start_server |
( |
str | Path | None | project_root = None, |
|
|
str | None | profile = None, |
|
|
Path | None | config_path = None, |
|
|
str | transport = "stdio", |
|
|
str | host = "127.0.0.1", |
|
|
int | port = 3001, |
|
|
int | None | metrics_port = None ) |
Initialise and run the ``oct-mcp`` server.
Steps:
1. Resolve and validate project root (Layer 1 — Gateway).
2. Load :class:`McpConfig` (with env var overrides).
3. Apply centralised policy overrides (Phase 5C, M-C7).
4. Override profile if ``--profile`` CLI arg was provided.
5. Instantiate all pipeline layers.
6. Start Prometheus metrics server (optional, Phase 5C).
7. Create FastMCP server.
8. Set ``_server_state`` singleton.
9. Register 9 read-only (Phase 5A) + 11 write (Phase 5B) tools.
10. Run the FastMCP server (stdio or SSE transport).
Parameters
----------
project_root
Project root path override (``None`` = auto-discover).
profile
MCP profile override (``None`` = use config file / env var).
config_path
Path to the ``~/.oct-mcp/config.json`` config file
(``None`` = use default location).
transport
MCP transport: ``"stdio"`` (default) or ``"sse"``
(HTTP/Server-Sent Events for remote access).
host
Host to bind for SSE transport (default ``"127.0.0.1"``).
Only used when *transport* is ``"sse"``.
port
Port to bind for SSE transport (default ``3001``).
Only used when *transport* is ``"sse"``.
metrics_port
If set, starts a Prometheus ``/metrics`` server on this port.
Overrides ``config.metrics_port``. Requires ``oct[metrics]``.
Definition at line 216 of file server.py.