66from fastmcp import Context
77
88from basic_memory .config import ConfigManager
9+ from basic_memory import telemetry
910from basic_memory .mcp .project_context import (
1011 detect_project_from_url_prefix ,
1112 get_project_client ,
@@ -190,8 +191,6 @@ async def build_context(
190191 if detected :
191192 project = detected
192193
193- logger .info (f"Building context from { url } in project { project } " )
194-
195194 # Convert string depth to integer if needed
196195 if isinstance (depth , str ):
197196 try :
@@ -203,25 +202,52 @@ async def build_context(
203202
204203 # URL is already validated and normalized by MemoryUrl type annotation
205204
206- async with get_project_client (project , workspace , context ) as (client , active_project ):
207- # Resolve memory:// identifier with project-prefix awareness
208- _ , resolved_path , _ = await resolve_project_and_path (client , url , project , context )
209-
210- # Import here to avoid circular import
211- from basic_memory .mcp .clients import MemoryClient
212-
213- # Use typed MemoryClient for API calls
214- memory_client = MemoryClient (client , active_project .external_id )
215- graph = await memory_client .build_context (
216- resolved_path ,
217- depth = depth or 1 ,
218- timeframe = timeframe ,
219- page = page ,
220- page_size = page_size ,
221- max_related = max_related ,
222- )
223-
224- if output_format == "text" :
225- return _format_context_markdown (graph , active_project .name )
226-
227- return graph .model_dump ()
205+ with telemetry .operation (
206+ "mcp.tool.build_context" ,
207+ entrypoint = "mcp" ,
208+ tool_name = "build_context" ,
209+ requested_project = project ,
210+ workspace_id = workspace ,
211+ depth = depth or 1 ,
212+ timeframe = timeframe ,
213+ page = page ,
214+ page_size = page_size ,
215+ max_related = max_related ,
216+ output_format = output_format ,
217+ is_memory_url = str (url ).startswith ("memory://" ),
218+ ):
219+ async with get_project_client (project , workspace , context ) as (client , active_project ):
220+ with telemetry .contextualize (
221+ project_name = active_project .name ,
222+ workspace_id = workspace ,
223+ tool_name = "build_context" ,
224+ ):
225+ logger .info (
226+ "Building context" ,
227+ url = str (url ),
228+ project = active_project .name ,
229+ depth = depth ,
230+ timeframe = timeframe ,
231+ )
232+
233+ # Resolve memory:// identifier with project-prefix awareness
234+ _ , resolved_path , _ = await resolve_project_and_path (client , url , project , context )
235+
236+ # Import here to avoid circular import
237+ from basic_memory .mcp .clients import MemoryClient
238+
239+ # Use typed MemoryClient for API calls
240+ memory_client = MemoryClient (client , active_project .external_id )
241+ graph = await memory_client .build_context (
242+ resolved_path ,
243+ depth = depth or 1 ,
244+ timeframe = timeframe ,
245+ page = page ,
246+ page_size = page_size ,
247+ max_related = max_related ,
248+ )
249+
250+ if output_format == "text" :
251+ return _format_context_markdown (graph , active_project .name )
252+
253+ return graph .model_dump ()
0 commit comments