Skip to content

BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.

fix(mcp): async request handling

Christiana1a73d7b2c34
diff --git a/README.md b/README.md
index 8f6a319..4644d5c 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
<p align="center">
<a href="https://discord.gg/VTngQTaeDf"><img src="https://img.shields.io/badge/Discord-Join%20Lumen%20Brain-5865F2?style=for-the-badge&logo=discord&logoColor=white" alt="Discord"/></a>
- <img src="https://img.shields.io/badge/version-2.7.12--dev-blue?style=for-the-badge" alt="Version"/>
+ <img src="https://img.shields.io/badge/version-2.7.13--dev-blue?style=for-the-badge" alt="Version"/>
<img src="https://img.shields.io/badge/python-3.11+-green?style=for-the-badge&logo=python&logoColor=white" alt="Python"/>
<img src="https://img.shields.io/badge/license-AGPLv3%20%2B%20Commons%20Clause-purple?style=for-the-badge" alt="License"/>
</p>
diff --git a/pyproject.toml b/pyproject.toml
index d16c5ee..b71837f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "brainapi2"
-version = "2.7.12-dev"
+version = "2.7.13-dev"
description = "Version 2.x.x of the BrainAPI memory layer."
authors = [
{name = "Christian",email = "[email protected]"}
diff --git a/src/services/mcp/app.py b/src/services/mcp/app.py
index a02fa52..31ee5fa 100644
--- a/src/services/mcp/app.py
+++ b/src/services/mcp/app.py
@@ -5,11 +5,12 @@ import dotenv
_project_root = Path(__file__).resolve().parent.parent.parent
dotenv.load_dotenv(_project_root / ".env")
+from contextlib import asynccontextmanager
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
from mcp.server.transport_security import TransportSecuritySettings
from starlette.applications import Starlette
from starlette.routing import Mount
-from starlette.types import Receive, Scope, Send
+from starlette.types import ASGIApp, Receive, Scope, Send
from src.services.mcp.main import mcp
transport_security = TransportSecuritySettings(
@@ -26,14 +27,20 @@ session_manager = StreamableHTTPSessionManager(
mcp._session_manager = session_manager
-class MCPApp:
+class MCPHandler:
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
await session_manager.handle_request(scope, receive, send)
+@asynccontextmanager
+async def lifespan(app: Starlette):
+ async with session_manager.run():
+ yield
+
+
app = Starlette(
routes=[
- Mount("/mcp", app=MCPApp()),
+ Mount("/", app=MCPHandler()),
],
- lifespan=lambda _: session_manager.run(),
+ lifespan=lifespan,
)