Skip to content

Commit c9b1764

Browse files
committed
mcp's sse
1 parent 9954000 commit c9b1764

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

.gemini/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"mcpServers": {
3+
"cape_sandbox": {
4+
"url": "http://127.0.0.1:9004/sse",
5+
"type": "sse"
6+
}
7+
}
8+
}

docs/book/src/usage/mcp.rst

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ Environment Variables
7878
* ``CAPE_API_URL``: (Optional) The full path to your CAPE API v2 endpoint (e.g., ``http://127.0.0.1:8000/apiv2``). If not set, it defaults to the ``url`` in ``api.conf`` + ``/apiv2``.
7979
* ``CAPE_API_TOKEN``: (Optional) Your API token. Recommended to set this in the **Client Configuration** (e.g. ``claude_desktop_config.json``) rather than your system's global environment variables to ensure isolation.
8080
* ``CAPE_ALLOWED_SUBMISSION_DIR``: (Optional) Restricts ``submit_file`` to a specific local directory for security. Defaults to the current working directory.
81+
* ``CAPE_MCP_TRANSPORT``: (Optional) The transport protocol to use (``stdio``, ``sse``, ``streamable-http``, ``http``). Defaults to ``stdio``.
82+
* ``CAPE_MCP_HOST``: (Optional) Host to bind for network transports. Defaults to ``127.0.0.1``.
83+
* ``CAPE_MCP_PORT``: (Optional) Port to bind for network transports. Defaults to ``8000``.
8184

8285
Granular Tool Control (``api.conf``)
8386
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -115,6 +118,7 @@ Scenario B: Remote / Shared Server (SSE)
115118

116119
In this mode, a single MCP server instance runs continuously and accepts connections from multiple clients over the network.
117120

121+
0. **Execution:** Start the server using ``python3 web/mcp_server.py --transport sse``.
118122
1. **Configuration:** Start the server **without** a ``CAPE_API_TOKEN`` environment variable.
119123
2. **Strict Mode:** Ensure ``token_auth_enabled = yes`` is set in ``conf/api.conf``.
120124
3. **Usage:** Users **must** provide their API token in the ``token`` argument for every tool call (e.g., ``submit_file(..., token="MyKey")``).
@@ -133,13 +137,45 @@ If ``token_auth_enabled = yes`` and no token is found in either location, the re
133137
Running the Server
134138
------------------
135139

136-
Standard execution
137-
~~~~~~~~~~~~~~~~~~
140+
Standard execution (Stdio)
141+
~~~~~~~~~~~~~~~~~~~~~~~~~~
138142

139143
.. code-block:: bash
140144
141145
CAPE_API_URL=http://your-cape-ip:8000/apiv2 CAPE_API_TOKEN=your_token python3 web/mcp_server.py
142146
147+
Remote / SSE execution
148+
~~~~~~~~~~~~~~~~~~~~~~
149+
150+
To run the server as a persistent service accessible over the network:
151+
152+
.. code-block:: bash
153+
154+
python3 web/mcp_server.py --transport sse --port 9004
155+
156+
Deployment behind Nginx
157+
~~~~~~~~~~~~~~~~~~~~~~~
158+
159+
When running behind an Nginx reverse proxy, you **must** expose both the ``/sse`` and ``/messages`` endpoints and disable buffering to allow the event stream to function.
160+
161+
.. code-block:: nginx
162+
163+
location /sse {
164+
proxy_pass http://127.0.0.1:9004/sse;
165+
proxy_http_version 1.1;
166+
proxy_set_header Connection "";
167+
proxy_buffering off;
168+
proxy_cache off;
169+
proxy_read_timeout 24h;
170+
}
171+
172+
location /messages {
173+
proxy_pass http://127.0.0.1:9004/messages;
174+
proxy_http_version 1.1;
175+
proxy_set_header Host $host;
176+
proxy_buffering off;
177+
}
178+
143179
Client Integrations
144180
-------------------
145181

web/mcp_server.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,16 @@ async def verify_auth(token: str = "") -> str:
562562
return json.dumps({"authenticated": True, "message": "Token is valid.", "user": "Authenticated User"}, indent=2)
563563

564564
if __name__ == "__main__":
565-
mcp.run()
565+
import argparse
566+
567+
parser = argparse.ArgumentParser(description="CAPE MCP Server")
568+
parser.add_argument("--transport", choices=["stdio", "sse", "streamable-http", "http"], default=os.environ.get("CAPE_MCP_TRANSPORT", "stdio"), help="Transport protocol (default: stdio)")
569+
parser.add_argument("--host", default=os.environ.get("CAPE_MCP_HOST", "127.0.0.1"), help="Host to bind for HTTP/SSE (default: 127.0.0.1)")
570+
parser.add_argument("--port", type=int, default=int(os.environ.get("CAPE_MCP_PORT", "8000")), help="Port to bind for HTTP/SSE (default: 8000)")
571+
args = parser.parse_args()
572+
573+
if args.transport in ["sse", "streamable-http", "http"]:
574+
print(f"Starting {args.transport} server on {args.host}:{args.port}", file=sys.stderr)
575+
mcp.run(transport=args.transport, host=args.host, port=args.port)
576+
else:
577+
mcp.run(transport="stdio")

0 commit comments

Comments
 (0)