AI Hints
Home›MCP Tutorial›Completions and Instructions
MCP · Lesson 46 of 120

Completions

Suggest argument values.

⏱ 2 min read▶ Video at 41:06🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

When a user types an argument, like a city name, it's helpful to suggest values. Completions let the server suggest them, just like autocomplete in a search box.

Completions
From the video: Completions
  • Typing an argument
  • Suggest values
  • Like autocomplete

Suggesting cities

The completion handler gets a reference, and the argument being typed. For the city argument of the store template, we return cities that start with what was typed. The client asks with the letter l. Run it. Leeds is suggested.

completions.pypython
import asyncio
from mcp import Client
from mcp.server.mcpserver import MCPServer
from mcp_types import Completion, ResourceTemplateReference
mcp = MCPServer("northwind")
CITIES = ["Leeds", "York", "Harrogate"]
@mcp.resource("store://{city}")
def store(city: str) -> str:
    return f"{city}: 9am to 5:30pm"

@mcp.completion()
async def complete(ref, argument, context):
    if argument.name == "city":
        return Completion(values=[c for c in CITIES if c.lower().startswith(argument.value.lower())])
async def main():
    async with Client(mcp) as client:
        ref = ResourceTemplateReference(type="ref/resource", uri="store://{city}")
        print((await client.complete(ref, {"name": "city", "value": "l"})).completion.values)
asyncio.run(main())
Output
['Leeds']

Completion tips

Completions work for prompt arguments and template parameters. Return a short list. Filter by what the user has typed. And they're only suggestions, so still check the final value.

keyvalue
Works forprompts and templates
Returna short list
Filterby typed text
Stillvalidate

Summary

Completions suggest argument values as the user types. Next, server instructions.

Key takeaways
  • @mcp.completion
  • Filter
  • Suggest
Next up: Server Instructions — a guide for the model