|
| 1 | +"""MCP client example showing completion usage. |
| 2 | +
|
| 3 | +This example demonstrates how to use the completion feature in MCP clients. |
| 4 | +cd to the `examples/snippets` directory and run: |
| 5 | + uv run completion-client |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import os |
| 10 | + |
| 11 | +from mcp import ClientSession, StdioServerParameters |
| 12 | +from mcp.client.stdio import stdio_client |
| 13 | +from mcp.types import PromptReference, ResourceTemplateReference |
| 14 | + |
| 15 | +# Create server parameters for stdio connection |
| 16 | +server_params = StdioServerParameters( |
| 17 | + command="uv", # Using uv to run the server |
| 18 | + args=["run", "server", "completion", "stdio"], # Server with completion support |
| 19 | + env={"UV_INDEX": os.environ.get("UV_INDEX", "")}, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +async def run(): |
| 24 | + """Run the completion client example.""" |
| 25 | + async with stdio_client(server_params) as (read, write): |
| 26 | + async with ClientSession(read, write) as session: |
| 27 | + # Initialize the connection |
| 28 | + await session.initialize() |
| 29 | + |
| 30 | + # List available resource templates |
| 31 | + templates = await session.list_resource_templates() |
| 32 | + print("Available resource templates:") |
| 33 | + for template in templates.resourceTemplates: |
| 34 | + print(f" - {template.uriTemplate}") |
| 35 | + |
| 36 | + # List available prompts |
| 37 | + prompts = await session.list_prompts() |
| 38 | + print("\nAvailable prompts:") |
| 39 | + for prompt in prompts.prompts: |
| 40 | + print(f" - {prompt.name}") |
| 41 | + |
| 42 | + # Complete resource template arguments |
| 43 | + if templates.resourceTemplates: |
| 44 | + template = templates.resourceTemplates[0] |
| 45 | + print(f"\nCompleting arguments for resource template: {template.uriTemplate}") |
| 46 | + |
| 47 | + # Complete without context |
| 48 | + result = await session.complete( |
| 49 | + ref=ResourceTemplateReference(type="ref/resource", uri=template.uriTemplate), |
| 50 | + argument={"name": "owner", "value": "model"}, |
| 51 | + ) |
| 52 | + print(f"Completions for 'owner' starting with 'model': {result.completion.values}") |
| 53 | + |
| 54 | + # Complete with context - repo suggestions based on owner |
| 55 | + result = await session.complete( |
| 56 | + ref=ResourceTemplateReference(type="ref/resource", uri=template.uriTemplate), |
| 57 | + argument={"name": "repo", "value": ""}, |
| 58 | + context_arguments={"owner": "modelcontextprotocol"}, |
| 59 | + ) |
| 60 | + print(f"Completions for 'repo' with owner='modelcontextprotocol': {result.completion.values}") |
| 61 | + |
| 62 | + # Complete prompt arguments |
| 63 | + if prompts.prompts: |
| 64 | + prompt_name = prompts.prompts[0].name |
| 65 | + print(f"\nCompleting arguments for prompt: {prompt_name}") |
| 66 | + |
| 67 | + result = await session.complete( |
| 68 | + ref=PromptReference(type="ref/prompt", name=prompt_name), |
| 69 | + argument={"name": "style", "value": ""}, |
| 70 | + ) |
| 71 | + print(f"Completions for 'style' argument: {result.completion.values}") |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + """Entry point for the completion client.""" |
| 76 | + asyncio.run(run()) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments