Skip to content

Commit e89137f

Browse files
authored
Merge pull request #634 from modelcontextprotocol/ihrpr/context-for-completable
Include context into completions
2 parents f822c12 + 5b875aa commit e89137f

File tree

6 files changed

+490
-23
lines changed

6 files changed

+490
-23
lines changed

README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Resources](#resources)
1212
- [Tools](#tools)
1313
- [Prompts](#prompts)
14+
- [Completions](#completions)
1415
- [Running Your Server](#running-your-server)
1516
- [stdio](#stdio)
1617
- [Streamable HTTP](#streamable-http)
@@ -150,6 +151,33 @@ server.registerResource(
150151
}]
151152
})
152153
);
154+
155+
// Resource with context-aware completion
156+
server.registerResource(
157+
"repository",
158+
new ResourceTemplate("github://repos/{owner}/{repo}", {
159+
list: undefined,
160+
complete: {
161+
// Provide intelligent completions based on previously resolved parameters
162+
repo: (value, context) => {
163+
if (context?.arguments?.["owner"] === "org1") {
164+
return ["project1", "project2", "project3"].filter(r => r.startsWith(value));
165+
}
166+
return ["default-repo"].filter(r => r.startsWith(value));
167+
}
168+
}
169+
}),
170+
{
171+
title: "GitHub Repository",
172+
description: "Repository information"
173+
},
174+
async (uri, { owner, repo }) => ({
175+
contents: [{
176+
uri: uri.href,
177+
text: `Repository: ${owner}/${repo}`
178+
}]
179+
})
180+
);
153181
```
154182

155183
### Tools
@@ -233,12 +261,14 @@ Tools can return `ResourceLink` objects to reference resources without embedding
233261
Prompts are reusable templates that help LLMs interact with your server effectively:
234262

235263
```typescript
264+
import { completable } from "@modelcontextprotocol/sdk/server/completable.js";
265+
236266
server.registerPrompt(
237267
"review-code",
238268
{
239269
title: "Code Review",
240270
description: "Review code for best practices and potential issues",
241-
arguments: { code: z.string() }
271+
argsSchema: { code: z.string() }
242272
},
243273
({ code }) => ({
244274
messages: [{
@@ -250,6 +280,68 @@ server.registerPrompt(
250280
}]
251281
})
252282
);
283+
284+
// Prompt with context-aware completion
285+
server.registerPrompt(
286+
"team-greeting",
287+
{
288+
title: "Team Greeting",
289+
description: "Generate a greeting for team members",
290+
argsSchema: {
291+
department: completable(z.string(), (value) => {
292+
// Department suggestions
293+
return ["engineering", "sales", "marketing", "support"].filter(d => d.startsWith(value));
294+
}),
295+
name: completable(z.string(), (value, context) => {
296+
// Name suggestions based on selected department
297+
const department = context?.arguments?.["department"];
298+
if (department === "engineering") {
299+
return ["Alice", "Bob", "Charlie"].filter(n => n.startsWith(value));
300+
} else if (department === "sales") {
301+
return ["David", "Eve", "Frank"].filter(n => n.startsWith(value));
302+
} else if (department === "marketing") {
303+
return ["Grace", "Henry", "Iris"].filter(n => n.startsWith(value));
304+
}
305+
return ["Guest"].filter(n => n.startsWith(value));
306+
})
307+
}
308+
},
309+
({ department, name }) => ({
310+
messages: [{
311+
role: "assistant",
312+
content: {
313+
type: "text",
314+
text: `Hello ${name}, welcome to the ${department} team!`
315+
}
316+
}]
317+
})
318+
);
319+
```
320+
321+
### Completions
322+
323+
MCP supports argument completions to help users fill in prompt arguments and resource template parameters. See the examples above for [resource completions](#resources) and [prompt completions](#prompts).
324+
325+
#### Client Usage
326+
327+
```typescript
328+
// Request completions for any argument
329+
const result = await client.complete({
330+
ref: {
331+
type: "ref/prompt", // or "ref/resource"
332+
name: "example" // or uri: "template://..."
333+
},
334+
argument: {
335+
name: "argumentName",
336+
value: "partial" // What the user has typed so far
337+
},
338+
context: { // Optional: Include previously resolved arguments
339+
arguments: {
340+
previousArg: "value"
341+
}
342+
}
343+
});
344+
253345
```
254346

255347
### Display Names and Metadata
@@ -805,6 +897,7 @@ const result = await client.callTool({
805897
arg1: "value"
806898
}
807899
});
900+
808901
```
809902

810903
### Proxy Authorization Requests Upstream

src/server/completable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export enum McpZodTypeKind {
1515

1616
export type CompleteCallback<T extends ZodTypeAny = ZodTypeAny> = (
1717
value: T["_input"],
18+
context?: {
19+
arguments?: Record<string, string>;
20+
},
1821
) => T["_input"][] | Promise<T["_input"][]>;
1922

2023
export interface CompletableDef<T extends ZodTypeAny = ZodTypeAny>

0 commit comments

Comments
 (0)