Tools

Protokoll-Revision: 2024-11-05

Das Model Context Protocol (MCP) ermöglicht es Servern, Tools bereitzustellen, die von Sprachmodellen aufgerufen werden können. Tools ermöglichen es Modellen, mit externen Systemen zu interagieren, wie dem Abfragen von Datenbanken, dem Aufrufen von APIs oder dem Durchführen von Berechnungen. Jedes Tool wird eindeutig durch einen Namen identifiziert und enthält Metadaten, die sein Schema beschreiben.

Benutzerinteraktionsmodell

Tools in MCP sind darauf ausgelegt, modellgesteuert zu sein, was bedeutet, dass das Sprachmodell Tools automatisch entdecken und aufrufen kann, basierend auf seinem kontextuellen Verständnis und den Prompts des Benutzers.

Implementierungen sind jedoch frei, Tools über jedes Schnittstellenmuster bereitzustellen, das ihren Bedürfnissen entspricht—das Protokoll selbst schreibt kein spezifisches Benutzerinteraktionsmodell vor.

Für Vertrauen & Sicherheit und Schutz SOLLTE immer ein Mensch in der Schleife sein, der die Möglichkeit hat, Tool-Aufrufe abzulehnen.

Anwendungen SOLLTEN:

  • Eine UI bereitstellen, die klar macht, welche Werkzeuge dem KI‑Modell bereitgestellt werden
  • Klare visuelle Indikatoren einfügen, wenn Tools aufgerufen werden
  • Bestätigungsaufforderungen für Operationen an den Benutzer stellen, um sicherzustellen, dass ein Mensch in der Schleife ist

Fähigkeiten

Server, die Tools unterstützen, MÜSSEN die tools-Fähigkeit deklarieren:

{
  "capabilities": {
    "tools": {
      "listChanged": true
    }
  }
}

listChanged indicates whether the server will emit notifications when the list of available tools changes.

Protocol Messages

Listing Tools

To discover available tools, clients send a tools/list request. This operation supports pagination.

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "tools": [
      {
        "name": "get_weather",
        "description": "Get current weather information for a location",
        "inputSchema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "City name or zip code"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}

Calling Tools

To invoke a tool, clients send a tools/call request:

Request:

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "get_weather",
    "arguments": {
      "location": "New York"
    }
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Current weather in New York:\nTemperature: 72°F\nConditions: Partly cloudy"
      }
    ],
    "isError": false
  }
}

List Changed Notification

When the list of available tools changes, servers that declared the listChanged capability SHOULD send a notification:

{
  "jsonrpc": "2.0",
  "method": "notifications/tools/list_changed"
}

Message Flow

  sequenceDiagram
    participant LLM
    participant Client
    participant Server

    Note over Client,Server: Discovery
    Client->>Server: tools/list
    Server-->>Client: List of tools

    Note over Client,LLM: Tool Selection
    LLM->>Client: Select tool to use

    Note over Client,Server: Invocation
    Client->>Server: tools/call
    Server-->>Client: Tool result
    Client->>LLM: Process result

    Note over Client,Server: Updates
    Server--)Client: tools/list_changed
    Client->>Server: tools/list
    Server-->>Client: Updated tools

Data Types

Tool

A tool definition includes:

  • name: Unique identifier for the tool
  • description: Human-readable description of functionality
  • inputSchema: JSON Schema defining expected parameters

Tool Result

Tool results can contain multiple content items of different types:

Text Content

{
  "type": "text",
  "text": "Tool result text"
}

Image Content

{
  "type": "image",
  "data": "base64-encoded-data",
  "mimeType": "image/png"
}

Embedded Resources

Resources MAY be embedded, to provide additional context or data, behind a URI that can be subscribed to or fetched again by the client later:

{
  "type": "resource",
  "resource": {
    "uri": "resource://example",
    "mimeType": "text/plain",
    "text": "Resource content"
  }
}

Error Handling

Tools use two error reporting mechanisms:

  1. Protocol Errors: Standard JSON-RPC errors for issues like:

    • Unknown tools
    • Invalid arguments
    • Server errors
  2. Tool Execution Errors: Reported in tool results with isError: true:

    • API failures
    • Invalid input data
    • Business logic errors

Example protocol error:

{
  "jsonrpc": "2.0",
  "id": 3,
  "error": {
    "code": -32602,
    "message": "Unknown tool: invalid_tool_name"
  }
}

Example tool execution error:

{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Failed to fetch weather data: API rate limit exceeded"
      }
    ],
    "isError": true
  }
}

Security Considerations

  1. Servers MUST:

    • Validate all tool inputs
    • Implement proper access controls
    • Rate limit tool invocations
    • Sanitize tool outputs
  2. Clients SHOULD:

    • Prompt for user confirmation on sensitive operations
    • Show tool inputs to the user before calling the server, to avoid malicious or accidental data exfiltration
    • Validate tool results before passing to LLM
    • Implement timeouts for tool calls
    • Log tool usage for audit purposes