Completion

Protocol Revision: 2024-11-05

The Model Context Protocol (MCP) provides a standardized way for servers to offer argument autocompletion suggestions for prompts and resource URIs. This enables rich, IDE-like experiences where users receive contextual suggestions while entering argument values.

User Interaction Model

Completion in MCP is designed to support interactive user experiences similar to IDE code completion.

For example, applications may show completion suggestions in a dropdown or popup menu as users type, with the ability to filter and select from available options.

However, implementations are free to expose completion through any interface pattern that suits their needs—the protocol itself does not mandate any specific user interaction model.

Protocol Messages

Requesting Completions

To get completion suggestions, clients send a completion/complete request specifying what is being completed through a reference type:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "completion/complete",
  "params": {
    "ref": {
      "type": "ref/prompt",
      "name": "code_review"
    },
    "argument": {
      "name": "language",
      "value": "py"
    }
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "completion": {
      "values": ["python", "pytorch", "pyside"],
      "total": 10,
      "hasMore": true
    }
  }
}

Reference Types

The protocol supports two types of completion references:

TypeDescriptionExample
ref/promptReferences a prompt by name{"type": "ref/prompt", "name": "code_review"}
ref/resourceReferences a resource URI{"type": "ref/resource", "uri": "file:///{path}"}

Completion Results

Servers return an array of completion values ranked by relevance, with:

  • Maximal 100 Einträge pro Antwort
  • Optionale Gesamtzahl verfügbarer Treffer
  • Boolean, der anzeigt, ob weitere Ergebnisse existieren

Message Flow

  sequenceDiagram
    participant Client
    participant Server

    Note over Client: User types argument
    Client->>Server: completion/complete
    Server-->>Client: Completion suggestions

    Note over Client: User continues typing
    Client->>Server: completion/complete
    Server-->>Client: Refined suggestions

Data Types

CompleteRequest

  • ref: A PromptReference or ResourceReference
  • argument: Object containing:
    • name: Argument name
    • value: Current value

CompleteResult

  • completion: Object containing:
    • values: Array of suggestions (max 100)
    • total: Optional total matches
    • hasMore: Additional results flag

Implementation Considerations

  1. Server SOLLEN:

    • Vorschläge nach Relevanz sortiert zurückgeben
    • Fuzzy‑Matching dort implementieren, wo sinnvoll
    • Completion‑Anfragen rate‑limiten
    • Alle Eingaben validieren
  2. Clients SOLLEN:

    • Schnelle Completion‑Anfragen entprellen
    • Completion‑Ergebnisse bei Bedarf cachen
    • Fehlende oder partielle Ergebnisse robust behandeln

Security

Implementierungen MÜSSEN:

  • Alle Completion‑Eingaben validieren
  • Angemessenes Rate‑Limiting implementieren
  • Zugriff auf sensible Vorschläge kontrollieren
  • Informationsabfluss über Completion verhindern