@@ -150,6 +150,33 @@ server.registerResource(
150
150
}]
151
151
})
152
152
);
153
+
154
+ // Resource with context-aware completion
155
+ server .registerResource (
156
+ " repository" ,
157
+ new ResourceTemplate (" github://repos/{owner}/{repo}" , {
158
+ list: undefined ,
159
+ complete: {
160
+ // Provide intelligent completions based on previously resolved parameters
161
+ repo : (value , context ) => {
162
+ if (context ?.arguments ?.[" owner" ] === " org1" ) {
163
+ return [" project1" , " project2" , " project3" ].filter (r => r .startsWith (value ));
164
+ }
165
+ return [" default-repo" ].filter (r => r .startsWith (value ));
166
+ }
167
+ }
168
+ }),
169
+ {
170
+ title: " GitHub Repository" ,
171
+ description: " Repository information"
172
+ },
173
+ async (uri , { owner , repo }) => ({
174
+ contents: [{
175
+ uri: uri .href ,
176
+ text: ` Repository: ${owner }/${repo } `
177
+ }]
178
+ })
179
+ );
153
180
```
154
181
155
182
### Tools
@@ -233,12 +260,14 @@ Tools can return `ResourceLink` objects to reference resources without embedding
233
260
Prompts are reusable templates that help LLMs interact with your server effectively:
234
261
235
262
``` typescript
263
+ import { completable } from " @modelcontextprotocol/sdk/server/completable.js" ;
264
+
236
265
server .registerPrompt (
237
266
" review-code" ,
238
267
{
239
268
title: " Code Review" ,
240
269
description: " Review code for best practices and potential issues" ,
241
- arguments : { code: z .string () }
270
+ argsSchema : { code: z .string () }
242
271
},
243
272
({ code }) => ({
244
273
messages: [{
@@ -250,6 +279,35 @@ server.registerPrompt(
250
279
}]
251
280
})
252
281
);
282
+
283
+ // Prompt with context-aware completion
284
+ server .registerPrompt (
285
+ " team-greeting" ,
286
+ {
287
+ title: " Team Greeting" ,
288
+ description: " Generate a greeting for team members" ,
289
+ argsSchema: {
290
+ // Completable arguments can use context for intelligent suggestions
291
+ name: completable (z .string (), (value , context ) => {
292
+ if (context ?.arguments ?.[" department" ] === " engineering" ) {
293
+ return [" Alice" , " Bob" , " Charlie" ].filter (n => n .startsWith (value ));
294
+ } else if (context ?.arguments ?.[" department" ] === " sales" ) {
295
+ return [" David" , " Eve" , " Frank" ].filter (n => n .startsWith (value ));
296
+ }
297
+ return [" Guest" ].filter (n => n .startsWith (value ));
298
+ })
299
+ }
300
+ },
301
+ ({ name }) => ({
302
+ messages: [{
303
+ role: " assistant" ,
304
+ content: {
305
+ type: " text" ,
306
+ text: ` Hello ${name }, welcome to the team! `
307
+ }
308
+ }]
309
+ })
310
+ );
253
311
```
254
312
255
313
### Display Names and Metadata
@@ -637,6 +695,57 @@ server.registerTool(
637
695
638
696
## Advanced Usage
639
697
698
+ ### Context-Aware Completions
699
+
700
+ MCP supports intelligent completions that can use previously resolved values as context. This is useful for creating dependent parameter completions where later parameters depend on earlier ones:
701
+
702
+ ``` typescript
703
+ import { completable } from " @modelcontextprotocol/sdk/server/completable.js" ;
704
+
705
+ // For resource templates
706
+ server .registerResource (
707
+ " database-query" ,
708
+ new ResourceTemplate (" db://{database}/{table}/{query}" , {
709
+ list: undefined ,
710
+ complete: {
711
+ // Table completions depend on the selected database
712
+ table : (value , context ) => {
713
+ const database = context ?.arguments ?.[" database" ];
714
+ if (database === " users_db" ) {
715
+ return [" profiles" , " sessions" , " preferences" ].filter (t => t .startsWith (value ));
716
+ } else if (database === " products_db" ) {
717
+ return [" items" , " categories" , " inventory" ].filter (t => t .startsWith (value ));
718
+ }
719
+ return [];
720
+ }
721
+ }
722
+ }),
723
+ metadata ,
724
+ handler
725
+ );
726
+
727
+ // For prompts with completable arguments
728
+ server .registerPrompt (
729
+ " api-request" ,
730
+ {
731
+ argsSchema: {
732
+ endpoint: z .string (),
733
+ // Method completions can be context-aware
734
+ method: completable (z .string (), (value , context ) => {
735
+ const endpoint = context ?.arguments ?.[" endpoint" ];
736
+ if (endpoint ?.includes (" /readonly/" )) {
737
+ return [" GET" ].filter (m => m .startsWith (value .toUpperCase ()));
738
+ }
739
+ return [" GET" , " POST" , " PUT" , " DELETE" ].filter (m => m .startsWith (value .toUpperCase ()));
740
+ })
741
+ }
742
+ },
743
+ handler
744
+ );
745
+ ```
746
+
747
+ The context object contains an ` arguments ` field with previously resolved parameter values, allowing you to provide more intelligent and contextual completions.
748
+
640
749
### Dynamic Servers
641
750
642
751
If you want to offer an initial set of tools/prompts/resources, but later add additional ones based on user action or external state change, you can add/update/remove them _ after_ the Server is connected. This will automatically emit the corresponding ` listChanged ` notifications:
0 commit comments