Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions hindsight-api/hindsight_api/api/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def __init__(self, app, memory: MemoryEngine):
self.app = app
self.memory = memory
self.mcp_server = create_mcp_server(memory)
self.mcp_app = self.mcp_server.http_app()
# Use transport='sse' with path=None to get routes at /sse and /messages
self.mcp_app = self.mcp_server.http_app(path=None, transport="sse")

async def __call__(self, scope, receive, send):
if scope["type"] != "http":
Expand Down Expand Up @@ -173,15 +174,15 @@ async def __call__(self, scope, receive, send):
new_scope = scope.copy()
new_scope["path"] = new_path

# Wrap send to rewrite the SSE endpoint URL to include bank_id
# Wrap send to rewrite the SSE endpoint URL to include full path
# The SSE app sends "event: endpoint\ndata: /messages\n" but we need
# the client to POST to /{bank_id}/messages instead
# the client to POST to /mcp/{bank_id}/messages (full path including mount)
async def send_wrapper(message):
if message["type"] == "http.response.body":
body = message.get("body", b"")
if body and b"/messages" in body:
# Rewrite /messages to /{bank_id}/messages in SSE endpoint event
body = body.replace(b"data: /messages", f"data: /{bank_id}/messages".encode())
if body and b"/mcp/messages/" in body:
# Rewrite /mcp/messages/ to /mcp/{bank_id}/messages/
body = body.replace(b"/mcp/messages/", f"/mcp/{bank_id}/messages/".encode())
message = {**message, "body": body}
await send(message)

Expand Down
Loading