refactor(backend/chat): Extract MCP server name constants to avoid hardcoded strings

This commit is contained in:
Zamil Majdy
2026-02-10 12:11:56 +04:00
parent 57da545e02
commit 587e11c60a
2 changed files with 10 additions and 4 deletions

View File

@@ -8,6 +8,8 @@ import logging
import re
from typing import Any, cast
from backend.api.features.chat.sdk.tool_adapter import MCP_TOOL_PREFIX
logger = logging.getLogger(__name__)
# Tools that are blocked entirely (CLI/system access)
@@ -136,7 +138,7 @@ def create_security_hooks(user_id: str | None) -> dict[str, Any]:
tool_input = cast(dict[str, Any], input_data.get("tool_input", {}))
# Strip MCP prefix for consistent validation
clean_name = tool_name.removeprefix("mcp__copilot__")
clean_name = tool_name.removeprefix(MCP_TOOL_PREFIX)
# Validate basic tool access
result = _validate_tool_access(clean_name, tool_input)
@@ -240,7 +242,7 @@ def create_strict_security_hooks(
tool_input = cast(dict[str, Any], input_data.get("tool_input", {}))
# Remove MCP prefix if present
clean_name = tool_name.removeprefix("mcp__copilot__")
clean_name = tool_name.removeprefix(MCP_TOOL_PREFIX)
if clean_name not in allowed_set:
logger.warning(f"Blocked non-whitelisted tool: {tool_name}")

View File

@@ -16,6 +16,10 @@ from backend.api.features.chat.tools.base import BaseTool
logger = logging.getLogger(__name__)
# MCP server naming - the SDK prefixes tool names as "mcp__{server_name}__{tool}"
MCP_SERVER_NAME = "copilot"
MCP_TOOL_PREFIX = f"mcp__{MCP_SERVER_NAME}__"
# Context variables to pass user/session info to tool execution
_current_user_id: ContextVar[str | None] = ContextVar("current_user_id", default=None)
_current_session: ContextVar[ChatSession | None] = ContextVar(
@@ -198,7 +202,7 @@ def create_copilot_mcp_server():
# Create the MCP server
server = create_sdk_mcp_server(
name="copilot",
name=MCP_SERVER_NAME,
version="1.0.0",
tools=sdk_tools,
)
@@ -211,7 +215,7 @@ def create_copilot_mcp_server():
# List of tool names for allowed_tools configuration
COPILOT_TOOL_NAMES = [f"mcp__copilot__{name}" for name in TOOL_REGISTRY.keys()]
COPILOT_TOOL_NAMES = [f"{MCP_TOOL_PREFIX}{name}" for name in TOOL_REGISTRY.keys()]
# Also export the raw tool names for flexibility
RAW_TOOL_NAMES = list(TOOL_REGISTRY.keys())