-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.py
More file actions
133 lines (109 loc) · 4.38 KB
/
Copy pathhttp_server.py
File metadata and controls
133 lines (109 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""Streamable HTTP wrapper for Tiny Memory MCP."""
from __future__ import annotations
import json
import os
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Any
from server import DEFAULT_DB, MemoryStore, TinyMemoryMcp, error_response
HOST = os.environ.get("HOST", "127.0.0.1")
PORT = int(os.environ.get("PORT", "8000"))
MCP_PATH = os.environ.get("MCP_PATH", "/mcp")
TOKEN = os.environ.get("MCP_BEARER_TOKEN", "")
DEFAULT_ALLOWED_ORIGINS = {
"https://chatgpt.com",
"https://chat.openai.com",
"http://localhost",
"http://127.0.0.1",
}
def allowed_origins() -> set[str]:
raw = os.environ.get("MCP_ALLOWED_ORIGINS", "")
if not raw.strip():
return DEFAULT_ALLOWED_ORIGINS
return {origin.strip().rstrip("/") for origin in raw.split(",") if origin.strip()}
class McpHttpHandler(BaseHTTPRequestHandler):
server_version = "TinyMemoryMCP/0.1"
@property
def mcp(self) -> TinyMemoryMcp:
return self.server.mcp # type: ignore[attr-defined]
def do_GET(self) -> None:
if self.path != MCP_PATH:
self.send_error(HTTPStatus.NOT_FOUND)
return
if not self._authorized() or not self._origin_allowed():
return
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "text/event-stream")
self.send_header("Cache-Control", "no-cache")
self.end_headers()
self.wfile.write(b": tiny-memory-mcp ready\n\n")
def do_POST(self) -> None:
if self.path != MCP_PATH:
self.send_error(HTTPStatus.NOT_FOUND)
return
if not self._authorized() or not self._origin_allowed():
return
length = int(self.headers.get("Content-Length", "0"))
raw_body = self.rfile.read(length)
try:
payload = json.loads(raw_body)
except json.JSONDecodeError as exc:
self._write_json(error_response(None, -32700, f"Parse error: {exc}"), HTTPStatus.BAD_REQUEST)
return
if isinstance(payload, list):
responses = [self.mcp.handle(item) for item in payload if isinstance(item, dict)]
responses = [response for response in responses if response is not None]
if responses:
self._write_json(responses)
else:
self.send_response(HTTPStatus.ACCEPTED)
self.end_headers()
return
if not isinstance(payload, dict):
self._write_json(error_response(None, -32600, "Invalid request"), HTTPStatus.BAD_REQUEST)
return
response = self.mcp.handle(payload)
if response is None:
self.send_response(HTTPStatus.ACCEPTED)
self.end_headers()
return
self._write_json(response)
def log_message(self, format: str, *args: Any) -> None:
return
def _authorized(self) -> bool:
if not TOKEN:
return True
expected = f"Bearer {TOKEN}"
if self.headers.get("Authorization") == expected:
return True
self.send_response(HTTPStatus.UNAUTHORIZED)
self.send_header("WWW-Authenticate", "Bearer")
self.end_headers()
return False
def _origin_allowed(self) -> bool:
origin = self.headers.get("Origin")
if origin is None:
return True
normalized = origin.rstrip("/")
if normalized in allowed_origins():
return True
self._write_json(error_response(None, -32000, "Origin not allowed"), HTTPStatus.FORBIDDEN)
return False
def _write_json(self, payload: Any, status: HTTPStatus = HTTPStatus.OK) -> None:
encoded = json.dumps(payload).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
def main() -> int:
db_path = Path(os.environ.get("TINY_MEMORY_DB", DEFAULT_DB))
httpd = ThreadingHTTPServer((HOST, PORT), McpHttpHandler)
httpd.mcp = TinyMemoryMcp(MemoryStore(db_path)) # type: ignore[attr-defined]
print(f"Tiny Memory MCP HTTP server listening on http://{HOST}:{PORT}{MCP_PATH}", flush=True)
httpd.serve_forever()
return 0
if __name__ == "__main__":
raise SystemExit(main())