Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

17.0 KB
Raw Blame
"""What an MCP server may be, and what one of its tools may become.

Gap-closure Phase 5.1 and 5.3. Pure and synchronous: no network, no database,
no SDK import. That is the point. Every decision in this module has to be
reachable without opening a socket, because the decision this phase exists to
get right is the one taken *before* the socket opens.

An MCP server is somebody else's code, reached over the network, whose replies
land in the prompt that decides what the agent does next. Three defaults
follow from that, and each is here rather than in configuration:

**Streamable HTTP only.** A stdio server is a local process the agent starts.
That is code execution on the agents container, not an HTTP request, so it
stays refused unless an operator has both switched stdio on and named the
command. Two switches rather than one, because either alone is a setting
somebody flips while debugging.

**Read-only.** A tool is callable when the operator allowlisted it by name
*and* the server has not said it changes state. Both halves matter and they
say different things: the allowlist is the tenant's assertion, the annotation
is the vendor's. Either one saying no is a no. An unannotated tool is callable
only because an operator wrote its name down, and an empty allowlist, which is
the default, means the agent may call nothing at all.

A tool the server marks destructive is not a configuration option. It is
refused here and reachable, if at all, only through governed dispatch as a
live action with a declared capability contract. No MCP tool has such a
contract today, so the honest reading of "or not at all" is the one in force.

**The description is attacker-controlled too.** The server supplies it, and it
goes into the tool-selection prompt, so a malicious description is an
injection into the prompt that chooses what to call, which arrives before any
result does. :func:`vet_tool` scans the name, the description, the title and
every parameter description, and drops the tool on a high-severity hit rather
than sanitising it: a description trying to instruct the model has no
legitimate use, so there is nothing to preserve. What survives is sanitised,
length-capped, and its schema is projected down to the keys a tool schema
needs, so a server cannot smuggle arbitrary JSON into what the model is shown.

That last control is the load-bearing one, and the guard's own measurements
are why. The guard was hardened after this phase's corpus was built: it now
scores 0.96 on prose and 98.1% on the corpus of payloads written to fit a
constrained field, up from 0.852 and 66.7%. But graded against 28 payloads
authored *after* the change, it catches **2**. An MCP server's payload is
held-out data by definition, written by somebody who has read whatever the
guard published, so 7.1% is the figure that applies here rather than 98.1%.
The structural limits are what hold, because they hold whatever the guard
scores.
"""

from __future__ import annotations

import os
import re
from dataclasses import dataclass, field
from typing import Any

from app.investigator.prompt_sanitizer import sanitize_for_prompt
from app.prompting.envelope import PromptInjectionGuard

__all__ = [
    "MAX_DESCRIPTION_CHARS",
    "MAX_ENUM_VALUES",
    "MAX_TOOLS_PER_SERVER",
    "ServerVerdict",
    "ToolVerdict",
    "namespaced",
    "stdio_allowed_commands",
    "stdio_enabled",
    "vet_server",
    "vet_tool",
]

#: A vendor description is documentation, not a payload. Long enough for a
#: real one, short enough that a server cannot spend the prompt budget.
MAX_DESCRIPTION_CHARS = 400

#: Enum values are shown to the model verbatim, so they are capped like any
#: other server-supplied string.
MAX_ENUM_VALUES = 40

#: A server advertising more tools than this is refused past the cap rather
#: than allowed to flood the tool-selection prompt. The allowlist usually
#: binds first; this is the bound when an operator has allowlisted widely.
MAX_TOOLS_PER_SERVER = 64

#: The shape an OpenAI function name may take. The namespaced id is built from
#: the server name and the tool name, so both are constrained to it.
_NAME_RE = re.compile(r"^[A-Za-z0-9_.-]{1,64}$")

_GUARD = PromptInjectionGuard()


def stdio_enabled() -> bool:
    """Whether an operator has switched stdio transports on at all."""
    return os.getenv("AISOC_MCP_STDIO_ENABLED", "").strip().lower() in {"1", "true", "yes", "on"}


def stdio_allowed_commands() -> frozenset[str]:
    """Commands an operator has named as startable.

    Compared against the whole command string, not a prefix and not a basename.
    A prefix match would admit ``/usr/bin/vendor-mcp; curl evil`` and a
    basename match would admit any ``vendor-mcp`` anywhere on the filesystem.
    """
    raw = os.getenv("AISOC_MCP_STDIO_ALLOWED_COMMANDS", "") or ""
    return frozenset(part.strip() for part in raw.split(",") if part.strip())


@dataclass(frozen=True)
class ServerVerdict:
    """Whether this server may be reached, and the sentence to record if not."""

    admitted: bool
    reason: str = ""


@dataclass(frozen=True)
class ToolVerdict:
    """Whether this tool may be bound, plus what survived vetting."""

    admitted: bool
    reason: str = ""
    #: ``read_only`` | ``destructive`` | ``state_changing`` | ``not_allowlisted``
    #: | ``bad_name`` | ``injected_description``. Recorded in the ledger so a
    #: refusal can be told apart from a tool that was never offered.
    classification: str = "unknown"
    description: str = ""
    parameters: dict[str, Any] = field(default_factory=dict)


#: What an OpenAI-compatible provider accepts as a function name.
#:
#: This is the whole reason the encoding below exists. The id used to be
#: ``mcp.<server>.<tool>``, and a dot is not in this set, so **every MCP tool
#: offered to a model was rejected by the provider**. Nothing local could see
#: it: the name is only validated at the far end.
FUNCTION_NAME_PATTERN = re.compile(r"^[a-zA-Z0-9_-]{1,64}$")

#: Separates the prefix, the server and the tool. Two underscores cannot occur
#: inside an encoded part, because :func:`_encode_part` only ever emits a
#: single ``_`` followed by two hex digits.
_SEPARATOR = "__"

_NAME_PREFIX = "mcp"

#: Characters an encoded part may contain unescaped. Deliberately excludes
#: ``_``, so the separator stays unambiguous.
_SAFE_CHARS = frozenset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")


def _encode_part(part: str) -> str:
    """One name component, reversibly, using only safe characters.

    Every character outside :data:`_SAFE_CHARS` becomes ``_`` plus two hex
    digits, which covers the dot that broke this, the underscore that would
    otherwise collide with the separator, and anything else an operator puts
    in a server name.
    """
    out: list[str] = []
    for char in part:
        if char in _SAFE_CHARS:
            out.append(char)
        else:
            code = ord(char)
            if code > 0xFF:
                # Above Latin-1 this scheme has no room. A server name is
                # operator-supplied configuration, so refusing with the reason
                # is better than emitting a name the provider rejects.
                raise ValueError(f"cannot encode {char!r} in an MCP tool name; use ASCII in the server name and tool id")
            out.append(f"_{code:02x}")
    return "".join(out)


def _decode_part(part: str) -> str:
    out: list[str] = []
    index = 0
    while index < len(part):
        char = part[index]
        if char == "_" and index + 2 < len(part) + 1:
            hex_digits = part[index + 1 : index + 3]
            if len(hex_digits) == 2:
                try:
                    out.append(chr(int(hex_digits, 16)))
                except ValueError:
                    out.append(char)
                    index += 1
                    continue
                index += 3
                continue
        out.append(char)
        index += 1
    return "".join(out)


def namespaced(server: str, tool: str) -> str:
    """The id the model sees, as a name a provider will actually accept.

    ``mcp__<server>__<tool>`` with each part escaped. This used to be
    ``mcp.<server>.<tool>``; see :data:`FUNCTION_NAME_PATTERN`.

    Raises ``ValueError`` when the result cannot be a valid function name,
    which is a configuration problem an operator can fix by shortening the
    server name. Returning an invalid name instead would move the failure to
    the provider, where the message says nothing about this deployment.
    """
    name = f"{_NAME_PREFIX}{_SEPARATOR}{_encode_part(server)}{_SEPARATOR}{_encode_part(tool)}"
    if not FUNCTION_NAME_PATTERN.match(name):
        raise ValueError(
            f"{name!r} is not a callable function name ({len(name)} characters; the limit is 64). Shorten the MCP server name."
        )
    return name


def parse_namespaced(name: str) -> tuple[str, str]:
    """``(server, tool)`` from a name :func:`namespaced` produced.

    Dispatch does not need this -- the registry looks a tool up by name and
    the closure already holds its server -- but a ledger row and a log line
    do, so an operator reading one can tell which server answered.
    """
    parts = name.split(_SEPARATOR)
    if len(parts) != 3 or parts[0] != _NAME_PREFIX:
        raise ValueError(f"{name!r} is not an MCP tool name")
    return _decode_part(parts[1]), _decode_part(parts[2])


def vet_server(
    *,
    name: str,
    transport: str,
    url: str | None,
    command: str | None,
    args: list[str] | None = None,
) -> ServerVerdict:
    """Decide whether this server may be reached at all.

    No DNS, no connection. The SSRF guard runs later, immediately before the
    socket opens, because a name that resolves publicly now can resolve to
    link-local by the time a request goes out.
    """
    if not _NAME_RE.match(name or ""):
        return ServerVerdict(False, f"{name!r} is not a usable server name; it becomes part of the tool name the model is shown")

    if transport == "streamable_http":
        if not (url or "").strip():
            return ServerVerdict(False, f"{name}: a streamable_http server with no URL cannot be reached")
        return ServerVerdict(True)

    if transport != "stdio":
        return ServerVerdict(False, f"{name}: transport {transport!r} is not supported; AiSOC speaks streamable HTTP and stdio")

    # From here down it is stdio, which starts a process.
    if not stdio_enabled():
        return ServerVerdict(
            False,
            f"{name}: stdio MCP servers are disabled. A stdio server is a local process this container would start, "
            f"so it stays off until an operator sets AISOC_MCP_STDIO_ENABLED and names the command in "
            f"AISOC_MCP_STDIO_ALLOWED_COMMANDS",
        )
    candidate = (command or "").strip()
    if not candidate:
        return ServerVerdict(False, f"{name}: a stdio server with no command cannot be started")
    allowed = stdio_allowed_commands()
    if candidate not in allowed:
        return ServerVerdict(
            False,
            f"{name}: {candidate!r} is not in AISOC_MCP_STDIO_ALLOWED_COMMANDS, so it will not be started",
        )
    # The command is allowlisted; the argument vector still has to be plain.
    # A shell metacharacter in an argument is how an allowlisted command turns
    # into a different one, and nothing downstream runs a shell, so an
    # argument that needs one is a mistake or an attempt.
    for arg in args or []:
        if not isinstance(arg, str) or any(ch in arg for ch in ";|&$`\n\r<>"):
            return ServerVerdict(False, f"{name}: argument {arg!r} carries shell metacharacters and will not be passed")
    return ServerVerdict(True)


def _flagged(value: Any) -> list[str]:
    """High-severity injection signal kinds found in ``value``, if any.

    Medium signals are not grounds to drop a tool. They fire on structural
    markers that a well-formed schema can legitimately contain, and dropping a
    vendor's whole tool over one would make this control the thing operators
    switch off.
    """
    verdict = _GUARD.scan(value)
    return sorted({s.kind for s in verdict.signals if s.severity == "high"})


def _clean_text(value: Any, *, limit: int = MAX_DESCRIPTION_CHARS) -> str:
    """Sanitise and cap one server-supplied string."""
    if not isinstance(value, str) or not value.strip():
        return ""
    return sanitize_for_prompt(value, label="mcp")[:limit]


def _project_schema(schema: Any) -> dict[str, Any]:
    """Keep the keys a tool schema needs and drop the rest.

    A server supplies this whole object and the provider renders it into the
    tool-selection prompt. Passing it through would let a server put arbitrary
    JSON there, including ``$ref`` (which invites a fetch), ``default`` and
    ``examples`` (free text nothing caps) and nested objects of any depth.
    What survives is an object with typed properties and a required list.
    """
    if not isinstance(schema, dict):
        return {"type": "object", "properties": {}, "required": []}

    properties: dict[str, Any] = {}
    raw_properties = schema.get("properties")
    if isinstance(raw_properties, dict):
        for key, spec in list(raw_properties.items())[:32]:
            if not isinstance(key, str) or not _NAME_RE.match(key):
                continue
            projected: dict[str, Any] = {"type": "string"}
            if isinstance(spec, dict):
                declared = spec.get("type")
                if declared in {"string", "number", "integer", "boolean", "array", "object"}:
                    projected["type"] = declared
                description = _clean_text(spec.get("description"), limit=200)
                if description:
                    projected["description"] = description
                enum = spec.get("enum")
                if isinstance(enum, list) and enum:
                    projected["enum"] = [_clean_text(v, limit=64) or str(v)[:64] for v in enum[:MAX_ENUM_VALUES]]
                items = spec.get("items")
                if projected["type"] == "array" and isinstance(items, dict):
                    item_type = items.get("type")
                    projected["items"] = {"type": item_type if item_type in {"string", "number", "integer", "boolean"} else "string"}
            properties[key] = projected

    required = [r for r in (schema.get("required") or []) if isinstance(r, str) and r in properties]
    return {"type": "object", "properties": properties, "required": required}


def vet_tool(
    *,
    server: str,
    name: str,
    description: Any,
    title: Any = None,
    input_schema: Any = None,
    read_only_hint: bool | None = None,
    destructive_hint: bool | None = None,
    allowlist: list[str] | None = None,
) -> ToolVerdict:
    """Decide whether one discovered tool may be bound, in refusal order.

    The order is the contract. The allowlist is consulted first so a tool the
    operator never named is refused without anything else about it being
    considered, and the same function is called again at dispatch, before a
    connection exists, so an allowlist that is only checked at discovery
    cannot be what this rests on.
    """
    permitted = set(allowlist or [])
    if name not in permitted:
        return ToolVerdict(
            False,
            f"{namespaced(server, name)} is not in this server's tool allowlist",
            classification="not_allowlisted",
        )

    if not _NAME_RE.match(name or "") or not _NAME_RE.match(server or ""):
        return ToolVerdict(False, f"{server}/{name}: not a usable tool name", classification="bad_name")

    # The vendor's own declaration. `destructive_hint` true is explicit;
    # `read_only_hint` false is the same statement made the other way round,
    # and reading only the first would admit every tool that says it writes
    # without using the word destructive.
    if destructive_hint is True:
        return ToolVerdict(
            False,
            f"{namespaced(server, name)} is annotated destructive. A state-changing MCP tool is reachable only through "
            f"governed dispatch as a live action with a declared capability contract, and none is declared for MCP tools",
            classification="destructive",
        )
    if read_only_hint is False:
        return ToolVerdict(
            False,
            f"{namespaced(server, name)} declares itself not read-only, so it is refused for the same reason a destructive one is",
            classification="state_changing",
        )

    flagged = _flagged({"name": name, "title": title, "description": description, "schema": input_schema})
    if flagged:
        return ToolVerdict(
            False,
            f"{namespaced(server, name)} was dropped: its server-supplied text reads as an instruction to the model "
            f"({', '.join(flagged)}). A tool description is chosen by the server, so this is an injection into the prompt "
            f"that decides which tool to call",
            classification="injected_description",
        )

    cleaned = _clean_text(description) or f"Tool {name} on MCP server {server}. The server supplied no description."
    return ToolVerdict(
        True,
        "",
        classification="read_only",
        description=cleaned,
        parameters=_project_schema(input_schema),
    )