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

Commit 4fa83de

Browse files
authored
feat: remove tools based on env vars (#162)
Based on #161, this PR adds the ability to conditionally deregister certain tools, using env vars with the schema `<tool_name>_DISABLED`. This PR just achieves it via using the low-level API, as opposed to conditionally declaring each function, which saves us some boilerplate and in my opinion produces better-looking code.
1 parent da67fec commit 4fa83de

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/semgrep_mcp/server.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,34 @@ async def health(request: Request) -> JSONResponse:
10881088
return JSONResponse({"status": "ok", "version": __version__})
10891089

10901090

1091+
# ---------------------------------------------------------------------------------
1092+
# Disabling tools
1093+
# ---------------------------------------------------------------------------------
1094+
1095+
TOOL_DISABLE_ENV_VARS = {
1096+
"SEMGREP_RULE_SCHEMA_DISABLED": "semgrep_rule_schema",
1097+
"GET_SUPPORTED_LANGUAGES_DISABLED": "get_supported_languages",
1098+
"SEMGREP_FINDINGS_DISABLED": "semgrep_findings",
1099+
"SEMGREP_SCAN_WITH_CUSTOM_RULE_DISABLED": "semgrep_scan_with_custom_rule",
1100+
"SEMGREP_SCAN_DISABLED": "semgrep_scan",
1101+
"SEMGREP_SCAN_LOCAL_DISABLED": "semgrep_scan_local",
1102+
"SECURITY_CHECK_DISABLED": "security_check",
1103+
"GET_ABSTRACT_SYNTAX_TREE_DISABLED": "get_abstract_syntax_tree",
1104+
"WRITE_CUSTOM_SEMGREP_RULE_DISABLED": "write_custom_semgrep_rule",
1105+
}
1106+
1107+
1108+
def deregister_tools() -> None:
1109+
for env_var, tool_name in TOOL_DISABLE_ENV_VARS.items():
1110+
is_disabled = os.environ.get(env_var, "false").lower() == "true"
1111+
1112+
if is_disabled:
1113+
# for the time being, while there is no way to API-level remove tools,
1114+
# we'll just mutate the internal `_tools`, because this language does
1115+
# not stop us from doing so
1116+
del mcp._tool_manager._tools[tool_name]
1117+
1118+
10911119
# ---------------------------------------------------------------------------------
10921120
# MCP Server Entry Point
10931121
# ---------------------------------------------------------------------------------
@@ -1127,6 +1155,9 @@ def main(transport: str, semgrep_path: str | None) -> None:
11271155
if semgrep_path:
11281156
set_semgrep_executable(semgrep_path)
11291157

1158+
# based on env vars, disable certain tools
1159+
deregister_tools()
1160+
11301161
if transport == "stdio":
11311162
mcp.run(transport="stdio")
11321163
elif transport == "streamable-http":

0 commit comments

Comments
 (0)