1313import httpx
1414from pydantic import Field
1515
16- from enrichmcp import EnrichContext , EnrichMCP , EnrichModel , Relationship
16+ from enrichmcp import EnrichMCP , EnrichModel , Relationship
1717
1818BACKEND_URL = "http://localhost:8001"
1919
@@ -69,42 +69,43 @@ class Order(EnrichModel):
6969 products : list [Product ] = Relationship (description = "Products in the order" )
7070
7171
72- async def _client (ctx : EnrichContext ) -> httpx .AsyncClient :
72+ async def _client () -> httpx .AsyncClient :
7373 """Helper to get the shared HTTP client."""
74+ ctx = app .get_context ()
7475 return ctx .request_context .lifespan_context ["client" ]
7576
7677
7778@app .retrieve
78- async def list_users (ctx : EnrichContext ) -> list [User ]:
79+ async def list_users () -> list [User ]:
7980 """Fetch all users from the backend service."""
80- client = await _client (ctx )
81+ client = await _client ()
8182 resp = await client .get ("/users" )
8283 resp .raise_for_status ()
8384 return [User (** u ) for u in resp .json ()]
8485
8586
8687@app .retrieve
87- async def get_user (user_id : int , ctx : EnrichContext ) -> User :
88+ async def get_user (user_id : int ) -> User :
8889 """Return a single user by ID."""
89- client = await _client (ctx )
90+ client = await _client ()
9091 resp = await client .get (f"/users/{ user_id } " )
9192 resp .raise_for_status ()
9293 return User (** resp .json ())
9394
9495
9596@app .retrieve
96- async def list_products (ctx : EnrichContext ) -> list [Product ]:
97+ async def list_products () -> list [Product ]:
9798 """Retrieve all products available for sale."""
98- client = await _client (ctx )
99+ client = await _client ()
99100 resp = await client .get ("/products" )
100101 resp .raise_for_status ()
101102 return [Product (** p ) for p in resp .json ()]
102103
103104
104105@app .retrieve
105- async def get_product (product_id : int , ctx : EnrichContext ) -> Product :
106+ async def get_product (product_id : int ) -> Product :
106107 """Get a single product by ID."""
107- client = await _client (ctx )
108+ client = await _client ()
108109 resp = await client .get (f"/products/{ product_id } " )
109110 resp .raise_for_status ()
110111 return Product (** resp .json ())
@@ -113,40 +114,37 @@ async def get_product(product_id: int, ctx: EnrichContext) -> Product:
113114@app .retrieve
114115async def list_orders (
115116 user_id : int | None = None ,
116- ctx : EnrichContext | None = None ,
117117) -> list [Order ]:
118118 """List orders optionally filtered by user."""
119- if ctx is None :
120- raise RuntimeError ("Context required" )
121- client = await _client (ctx )
119+ client = await _client ()
122120 params = {"user_id" : user_id } if user_id is not None else None
123121 resp = await client .get ("/orders" , params = params )
124122 resp .raise_for_status ()
125123 return [Order (** o ) for o in resp .json ()]
126124
127125
128126@app .retrieve
129- async def get_order (order_id : int , ctx : EnrichContext ) -> Order :
127+ async def get_order (order_id : int ) -> Order :
130128 """Retrieve a specific order."""
131- client = await _client (ctx )
129+ client = await _client ()
132130 resp = await client .get (f"/orders/{ order_id } " )
133131 resp .raise_for_status ()
134132 return Order (** resp .json ())
135133
136134
137135@User .orders .resolver
138- async def get_orders_for_user (user_id : int , ctx : EnrichContext ) -> list ["Order" ]:
139- return await list_orders (user_id = user_id , ctx = ctx )
136+ async def get_orders_for_user (user_id : int ) -> list ["Order" ]:
137+ return await list_orders (user_id = user_id )
140138
141139
142140@Order .user .resolver
143- async def get_order_user (user_id : int , ctx : EnrichContext ) -> "User" :
144- return await get_user (user_id = user_id , ctx = ctx )
141+ async def get_order_user (user_id : int ) -> "User" :
142+ return await get_user (user_id = user_id )
145143
146144
147145@Order .products .resolver
148- async def get_order_products (order_id : int , ctx : EnrichContext ) -> list [Product ]:
149- client = await _client (ctx )
146+ async def get_order_products (order_id : int ) -> list [Product ]:
147+ client = await _client ()
150148 resp = await client .get (f"/orders/{ order_id } " )
151149 resp .raise_for_status ()
152150 data = resp .json ()
0 commit comments