Skip to content
This repository was archived by the owner on Oct 28, 2025. It is now read-only.

Commit aa70e5b

Browse files
committed
link telemetry to ocaml server
1 parent f96dba9 commit aa70e5b

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/semgrep_mcp/semgrep.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ async def send_request(self, request: str, **kwargs: Any) -> str:
175175
################################################################################
176176

177177

178-
async def run_semgrep_process(args: list[str]) -> asyncio.subprocess.Process:
178+
async def run_semgrep_process(
179+
top_level_span: trace.Span | None, args: list[str]
180+
) -> asyncio.subprocess.Process:
179181
"""
180182
Runs semgrep with the given arguments as a subprocess, without waiting for it to finish.
181183
"""
@@ -185,7 +187,14 @@ async def run_semgrep_process(args: list[str]) -> asyncio.subprocess.Process:
185187

186188
# Just so we get the debug logs for the MCP server
187189
env = os.environ.copy()
188-
env["SEMGREP_LOG_SRCS"] = "mcp"
190+
env["SEMGREP_LOG_SRCS"] = "mcp,commons"
191+
if top_level_span:
192+
env["SEMGREP_TRACE_PARENT_SPAN_ID"] = trace.format_span_id(
193+
top_level_span.get_span_context().span_id
194+
)
195+
env["SEMGREP_TRACE_PARENT_TRACE_ID"] = trace.format_trace_id(
196+
top_level_span.get_span_context().trace_id
197+
)
189198

190199
# Execute semgrep command
191200
process = await asyncio.create_subprocess_exec(
@@ -202,7 +211,7 @@ async def run_semgrep_process(args: list[str]) -> asyncio.subprocess.Process:
202211
return process
203212

204213

205-
async def run_semgrep(args: list[str]) -> str:
214+
async def run_semgrep(top_level_span: trace.Span | None, args: list[str]) -> str:
206215
"""
207216
Runs semgrep with the given arguments
208217
@@ -213,7 +222,7 @@ async def run_semgrep(args: list[str]) -> str:
213222
Output of semgrep command
214223
"""
215224

216-
process = await run_semgrep_process(args)
225+
process = await run_semgrep_process(top_level_span, args)
217226

218227
stdout, stderr = await process.communicate()
219228

src/semgrep_mcp/server.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ async def server_lifespan(_server: Server) -> AsyncIterator[SemgrepContext]:
284284
# Initialize resources on startup with tracing
285285
# MCP requires Pro Engine
286286
with start_tracing("mcp-python-server") as span:
287-
process = await run_semgrep_process(["mcp", "--pro", "--debug"])
287+
process = await run_semgrep_process(top_level_span=span, args=["mcp", "--pro", "--debug"])
288288

289289
try:
290290
yield SemgrepContext(process=process, top_level_span=span)
@@ -350,7 +350,7 @@ async def get_supported_languages() -> list[str]:
350350
args = ["show", "supported-languages", "--experimental"]
351351

352352
# Parse output and return list of languages
353-
languages = await run_semgrep(args)
353+
languages = await run_semgrep(top_level_span=None, args=args)
354354
return [lang.strip() for lang in languages.strip().split("\n") if lang.strip()]
355355

356356

@@ -586,7 +586,7 @@ async def semgrep_scan_with_custom_rule(
586586

587587
# Run semgrep scan with custom rule
588588
args = get_semgrep_scan_args(temp_dir, rule_file_path)
589-
output = await run_semgrep(args)
589+
output = await run_semgrep(top_level_span=None, args=args)
590590
results: SemgrepScanResult = SemgrepScanResult.model_validate_json(output)
591591
remove_temp_dir_from_results(results, temp_dir)
592592
return results
@@ -631,7 +631,7 @@ async def semgrep_scan(
631631
# Create temporary files from code content
632632
temp_dir = create_temp_files_from_code_content(code_files)
633633
args = get_semgrep_scan_args(temp_dir, config)
634-
output = await run_semgrep(args)
634+
output = await run_semgrep(top_level_span=None, args=args)
635635
results: SemgrepScanResult = SemgrepScanResult.model_validate_json(output)
636636
remove_temp_dir_from_results(results, temp_dir)
637637
return results
@@ -731,7 +731,7 @@ async def semgrep_scan_local(
731731
results = []
732732
for cf in code_files:
733733
args = get_semgrep_scan_args(cf.path, config)
734-
output = await run_semgrep(args)
734+
output = await run_semgrep(top_level_span=None, args=args)
735735
result: SemgrepScanResult = SemgrepScanResult.model_validate_json(output)
736736
results.append(result)
737737
return results
@@ -787,7 +787,7 @@ async def security_check(
787787
# Create temporary files from code content
788788
temp_dir = create_temp_files_from_code_content(code_files)
789789
args = get_semgrep_scan_args(temp_dir)
790-
output = await run_semgrep(args)
790+
output = await run_semgrep(top_level_span=None, args=args)
791791
results: SemgrepScanResult = SemgrepScanResult.model_validate_json(output)
792792
remove_temp_dir_from_results(results, temp_dir)
793793

@@ -850,7 +850,7 @@ async def get_abstract_syntax_tree(
850850
"--json",
851851
temp_file_path,
852852
]
853-
return await run_semgrep(args)
853+
return await run_semgrep(top_level_span=None, args=args)
854854

855855
except McpError as e:
856856
raise e

0 commit comments

Comments
 (0)