Skip to content

Commit e7f98f1

Browse files
authored
Add get_context helper (#102)
* Add get_context method and tests * Fix get_context instantiation * docs: document get_context
1 parent 87344cb commit e7f98f1

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Added
10+
- `EnrichMCP.get_context()` returns an `EnrichContext` instance mirroring
11+
the underlying FastMCP context.
912

1013
## [0.4.4] - 2025-06-26
1114

docs/api/app.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ async def delete_user(uid: int) -> bool:
9595
...
9696
```
9797

98+
### `get_context() -> EnrichContext`
99+
100+
Return the current request context as an :class:`~enrichmcp.EnrichContext`.
101+
102+
```python
103+
app = EnrichMCP("My API", description="desc")
104+
ctx = app.get_context()
105+
assert ctx.fastmcp is app.mcp
106+
```
107+
98108
### `run(**options)`
99109

100110
Start the MCP server.

src/enrichmcp/app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from mcp.server.fastmcp import FastMCP
2121
from pydantic import BaseModel, Field, create_model
2222

23+
from .context import EnrichContext
2324
from .entity import EnrichModel
2425
from .relationship import Relationship
2526

@@ -428,6 +429,15 @@ def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
428429
return decorator(func)
429430
return cast("DecoratorCallable", decorator)
430431

432+
def get_context(self) -> EnrichContext:
433+
"""Return the current :class:`EnrichContext` for this app."""
434+
435+
base_ctx = self.mcp.get_context()
436+
return EnrichContext.model_construct(
437+
_request_context=getattr(base_ctx, "_request_context", None),
438+
_fastmcp=getattr(base_ctx, "_fastmcp", None),
439+
)
440+
431441
def run(
432442
self, *, transport: str | None = None, mount_path: str | None = None, **options: Any
433443
) -> Any:

tests/test_core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from unittest.mock import patch
2+
13
import pytest
24
from pydantic import Field
35

46
from enrichmcp import (
7+
EnrichContext,
58
EnrichMCP,
69
EnrichModel,
710
)
@@ -153,3 +156,26 @@ async def test_entity_without_description_fails():
153156
class BadEntity(EnrichModel):
154157
# No docstring, should fail
155158
id: int = Field(description="ID")
159+
160+
161+
def test_get_context_returns_enrich_context():
162+
"""app.get_context should return an EnrichContext"""
163+
164+
app = EnrichMCP("Test API", description="Test API description")
165+
ctx = app.get_context()
166+
167+
assert isinstance(ctx, EnrichContext)
168+
assert ctx.fastmcp is app.mcp
169+
170+
with pytest.raises(ValueError):
171+
_ = ctx.request_context
172+
173+
174+
def test_get_context_propagates_errors():
175+
app = EnrichMCP("Test API", description="desc")
176+
177+
with (
178+
patch.object(app.mcp, "get_context", side_effect=RuntimeError("boom")),
179+
pytest.raises(RuntimeError),
180+
):
181+
app.get_context()

0 commit comments

Comments
 (0)