Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Detach the MCP session context instead of leaving it attached after the client transport exits
([#280](https://github.com/alibaba/loongsuite-python/pull/280))

## Version 0.9.0 (2026-09-07)

There are no changelog entries for this release.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _safe_attach(self, context: Any):

def _safe_detach(self, context: Any):
try:
if context is None or hasattr(context[0], "_session_context"):
if context is None or not hasattr(context[0], "_session_context"):
return
delattr(context[0], "_session_context")
except Exception:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
from contextlib import asynccontextmanager
from types import SimpleNamespace

import pytest
from mcp import JSONRPCRequest
Expand All @@ -25,6 +26,7 @@
from opentelemetry import context, propagate
from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.mcp import MCPInstrumentor, ServerMetrics
from opentelemetry.instrumentation.mcp.session_handler import _SessionContext
from opentelemetry.metrics import get_meter
from opentelemetry.trace import SpanKind

Expand All @@ -38,6 +40,25 @@ async def mock_client(params):
yield params


def test_session_context_detach_removes_attached_context():
target = SimpleNamespace()
session_context = _SessionContext({})
context = (target,)
session_context._safe_attach(context)
assert hasattr(target, "_session_context")

session_context._safe_detach(context)

assert not hasattr(target, "_session_context")


def test_session_context_detach_without_attachment_does_not_raise():
target = SimpleNamespace()
session_context = _SessionContext({})
session_context._safe_detach((target,))
assert not hasattr(target, "_session_context")


@pytest.mark.asyncio
async def test_client_invalid(tracer_provider):
import mcp.client.sse # noqa: PLC0415
Expand Down
Loading