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): fix oauth redirect

Christiand7e9afbcc7d4
diff --git a/.env.example b/.env.example
index e487fba..15ac844 100644
--- a/.env.example
+++ b/.env.example
@@ -128,4 +128,16 @@ PLUGIN_PUBLISHER_ID="your-publisher-id"
# MCP
MCP_OAUTH_ISSUER_URL="https://your-mcp-oauth-issuer-url.com"
-MCP_RESOURCE_SERVER_URL="https://your-mcp-resource-server-url.com"
\ No newline at end of file
+MCP_RESOURCE_SERVER_URL="https://your-mcp-resource-server-url.com"
+
+# Tracing
+TRACE_TRACKER_ENABLED=false
+TRACE_QUEUE_MAX_SIZE=10000
+TRACE_SLOW_REQUEST_MS=1000
+TRACE_AGENT_SLOW_OPERATION_MS=30000
+TRACE_HEARTBEAT_INTERVAL_SECONDS=30
+TRACE_HEALTH_INTERVAL_SECONDS=30
+TRACE_RESOURCE_INTERVAL_SECONDS=30
+TRACE_HEALTH_TIMEOUT_SECONDS=1
+TRACE_AGENT_LOOP_ITERATIONS=20
+TRACE_AGENT_TOOL_LOOP_ITERATIONS=20
\ No newline at end of file
diff --git a/README.md b/README.md
index d3e24f5..64d1e92 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.11.9--dev-blue?style=for-the-badge" alt="Version"/>
+ <img src="https://img.shields.io/badge/version-2.11.10--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 fec0eb0..5c908d1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "brainapi2"
-version = "2.11.9-dev"
+version = "2.11.10-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 c9ca2bc..d1fe0fa 100644
--- a/src/services/mcp/app.py
+++ b/src/services/mcp/app.py
@@ -16,7 +16,7 @@ from contextlib import asynccontextmanager
from src.lib.tracing.middleware import TraceMiddleware
from src.lib.tracing.runtime import start_runtime_monitoring, stop_runtime_monitoring
-from src.services.mcp.main import auth_token_var, mcp
+from src.services.mcp.main import auth_token_var, mcp, oauth_provider
PLUGINS_DIR = Path(os.getenv("PLUGINS_DIR", str(_project_root / "plugins")))
diff --git a/src/services/mcp/main.py b/src/services/mcp/main.py
index e694251..b37e66d 100644
--- a/src/services/mcp/main.py
+++ b/src/services/mcp/main.py
@@ -93,6 +93,7 @@ if oauth_provider:
scope = q.get("scope") or " ".join(_oauth_scopes)
resource = q.get("resource") or ""
state = q.get("state") or ""
+ redirect_uri_provided = q.get("redirect_uri_provided_explicitly") or "1"
if not (client_id and redirect_uri and code_challenge):
return HTMLResponse("Missing OAuth parameters", status_code=400)
client = await oauth_provider.get_client(client_id)
@@ -111,6 +112,7 @@ if oauth_provider:
<input type="hidden" name="scope" value="{esc(scope)}">
<input type="hidden" name="resource" value="{esc(resource)}">
<input type="hidden" name="state" value="{esc(state)}">
+<input type="hidden" name="redirect_uri_provided_explicitly" value="{redirect_uri_provided}">
<label for="brainpat">BrainPAT</label>
<input id="brainpat" name="brainpat" type="password" autocomplete="off" required style="width:100%;max-width:40em">
<button type="submit">Authorize</button>
@@ -125,6 +127,9 @@ if oauth_provider:
scope_str = str(form.get("scope") or "")
resource = str(form.get("resource") or "") or None
state = str(form.get("state") or "") or None
+ redirect_uri_provided_explicitly = str(
+ form.get("redirect_uri_provided_explicitly") or "1"
+ ) == "1"
brainpat = str(form.get("brainpat") or "").strip()
if not (client_id and redirect_uri and code_challenge and brainpat):
return HTMLResponse("Missing fields", status_code=400)
@@ -145,6 +150,7 @@ if oauth_provider:
code = oauth_provider.issue_auth_code(
client_id=client_id,
redirect_uri=ru,
+ redirect_uri_provided_explicitly=redirect_uri_provided_explicitly,
code_challenge=code_challenge,
scopes=scopes,
resource=resource,
diff --git a/src/services/mcp/oauth_provider.py b/src/services/mcp/oauth_provider.py
index 5a9da6d..6f31e95 100644
--- a/src/services/mcp/oauth_provider.py
+++ b/src/services/mcp/oauth_provider.py
@@ -142,6 +142,9 @@ class BrainapiMcpOAuthProvider(
"client_id": client.client_id or "",
"redirect_uri": str(params.redirect_uri),
"code_challenge": params.code_challenge,
+ "redirect_uri_provided_explicitly": "1"
+ if params.redirect_uri_provided_explicitly
+ else "0",
"scope": " ".join(scopes),
}
if params.resource:
@@ -283,6 +286,7 @@ class BrainapiMcpOAuthProvider(
*,
client_id: str,
redirect_uri: AnyUrl,
+ redirect_uri_provided_explicitly: bool = True,
code_challenge: str,
scopes: list[str],
resource: str | None,
@@ -297,7 +301,7 @@ class BrainapiMcpOAuthProvider(
client_id=client_id,
code_challenge=code_challenge,
redirect_uri=redirect_uri,
- redirect_uri_provided_explicitly=True,
+ redirect_uri_provided_explicitly=redirect_uri_provided_explicitly,
resource=resource,
)
self._set_model(self._auth_code_key(code), ac, self._auth_code_ttl)
diff --git a/src/workers/app.py b/src/workers/app.py
index e5896e2..a013063 100644
--- a/src/workers/app.py
+++ b/src/workers/app.py
@@ -32,7 +32,7 @@ from kombu import Queue
from src.config import config
from src.lib.tracing import start_runtime_monitoring
-from src.lib.tracing.workers import register_celery_tracing
+from src.lib.tracing.workers import install_celery_tracing
os.environ.setdefault("GRPC_ENABLE_FORK_SUPPORT", "1")
@@ -100,5 +100,5 @@ ingestion_app.conf.update(
"fanout_patterns": True,
},
)
-register_celery_tracing(service_name="brainapi-worker")
+install_celery_tracing(service_name="brainapi-worker")
start_runtime_monitoring(service_name="brainapi-worker")
diff --git a/tests/test_mcp_oauth_redis_storage.py b/tests/test_mcp_oauth_redis_storage.py
index 8ccd0a1..ae3a136 100644
--- a/tests/test_mcp_oauth_redis_storage.py
+++ b/tests/test_mcp_oauth_redis_storage.py
@@ -1,12 +1,14 @@
import asyncio
import os
import unittest
+from urllib.parse import parse_qs, urlparse
from pydantic import AnyUrl
os.environ.setdefault("REDIS_HOST", "localhost")
os.environ.setdefault("REDIS_PORT", "6379")
+from mcp.server.auth.provider import AuthorizationParams
from mcp.shared.auth import OAuthClientInformationFull
from src.services.mcp.oauth_provider import BrainapiMcpOAuthProvider
@@ -99,6 +101,44 @@ class BrainapiMcpOAuthProviderRedisTests(unittest.TestCase):
self.assertIsNone(asyncio.run(recreated.load_authorization_code(loaded_client, code)))
self.assertIsNotNone(asyncio.run(recreated.load_access_token(token.access_token)))
+ def test_authorization_code_preserves_implicit_redirect_uri(self):
+ redis_client = FakeRedis()
+ provider = make_provider(redis_client)
+
+ code = provider.issue_auth_code(
+ client_id="client-1",
+ redirect_uri=AnyUrl("https://claude.ai/api/mcp/auth_callback"),
+ redirect_uri_provided_explicitly=False,
+ code_challenge="challenge",
+ scopes=["brainapi"],
+ resource="https://brainapi.example/mcp",
+ state="state",
+ brainpat="brain-pat",
+ )
+ auth_code = asyncio.run(provider.load_authorization_code(None, code))
+
+ self.assertFalse(auth_code.redirect_uri_provided_explicitly)
+
+ def test_authorize_carries_redirect_uri_explicitness_to_consent(self):
+ provider = make_provider(FakeRedis())
+ client = OAuthClientInformationFull(
+ client_id="client-1",
+ redirect_uris=[AnyUrl("https://claude.ai/api/mcp/auth_callback")],
+ )
+ params = AuthorizationParams(
+ state="state",
+ scopes=["brainapi"],
+ code_challenge="challenge",
+ redirect_uri=AnyUrl("https://claude.ai/api/mcp/auth_callback"),
+ redirect_uri_provided_explicitly=False,
+ resource="https://brainapi.example/mcp",
+ )
+
+ url = asyncio.run(provider.authorize(client, params))
+ query = parse_qs(urlparse(url).query)
+
+ self.assertEqual(query["redirect_uri_provided_explicitly"], ["0"])
+
def test_refresh_exchange_survives_provider_recreation_and_revokes_old_access_token(self):
redis_client = FakeRedis()
provider = make_provider(redis_client)