Skip to content

Commit d31f7fa

Browse files
author
Gavin Aboulhosn
committed
refactor: Add generic type safety for completions
Improve type safety for completions in both prompts and resources: - Add generic type parameters to PromptProtocol and ResourceProtocol - Add type-safe argument completion methods with proper inference - Update MCPServer to handle typed protocols - Add schema validation in completion methods This ensures completion arguments and values maintain proper typing throughout the completion flow.
1 parent e13f646 commit d31f7fa

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/core/MCPServer.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ export class MCPServer {
255255
const { ref, argument } = request.params;
256256

257257
if (ref.type === "ref/prompt") {
258-
const prompt = this.promptsMap.get(ref.name);
258+
const prompt = this.promptsMap.get(ref.name) as
259+
| PromptProtocol<any>
260+
| undefined;
259261
if (!prompt?.complete) {
260262
return { completion: { values: [] } };
261263
}
@@ -265,7 +267,9 @@ export class MCPServer {
265267
}
266268

267269
if (ref.type === "ref/resource") {
268-
const resource = this.resourcesMap.get(ref.uri);
270+
const resource = this.resourcesMap.get(ref.uri) as
271+
| ResourceProtocol<any>
272+
| undefined;
269273
if (!resource?.complete) {
270274
return { completion: { values: [] } };
271275
}

src/prompts/BasePrompt.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type PromptCompletion = {
1818
hasMore?: boolean;
1919
};
2020

21-
export interface PromptProtocol {
21+
export interface PromptProtocol<TArgs extends Record<string, any> = {}> {
2222
name: string;
2323
description: string;
2424
promptDefinition: {
@@ -30,7 +30,7 @@ export interface PromptProtocol {
3030
required?: boolean;
3131
}>;
3232
};
33-
getMessages(args?: Record<string, unknown>): Promise<
33+
getMessages(args?: Partial<TArgs>): Promise<
3434
Array<{
3535
role: string;
3636
content: {
@@ -44,11 +44,14 @@ export interface PromptProtocol {
4444
};
4545
}>
4646
>;
47-
complete?(argument: string, name: string): Promise<PromptCompletion>;
47+
complete?<K extends keyof TArgs & string>(
48+
argumentName: K,
49+
value: string,
50+
): Promise<PromptCompletion>;
4851
}
4952

5053
export abstract class MCPPrompt<TArgs extends Record<string, any> = {}>
51-
implements PromptProtocol
54+
implements PromptProtocol<TArgs>
5255
{
5356
abstract name: string;
5457
abstract description: string;
@@ -92,10 +95,14 @@ export abstract class MCPPrompt<TArgs extends Record<string, any> = {}>
9295
return this.generateMessages(validatedArgs);
9396
}
9497

95-
async complete?(
96-
argumentName: string,
98+
async complete<K extends keyof TArgs & string>(
99+
argumentName: K,
97100
value: string,
98101
): Promise<PromptCompletion> {
102+
if (!this.schema[argumentName].type) {
103+
throw new Error(`No schema found for argument: ${argumentName}`);
104+
}
105+
99106
return {
100107
values: [],
101108
total: 0,

src/resources/BaseResource.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type ResourceCompletion = {
2525
hasMore?: boolean;
2626
};
2727

28-
export interface ResourceProtocol {
28+
export interface ResourceProtocol<TArgs extends Record<string, any> = {}> {
2929
uri: string;
3030
name: string;
3131
description?: string;
@@ -35,10 +35,15 @@ export interface ResourceProtocol {
3535
read(): Promise<ResourceContent[]>;
3636
subscribe?(): Promise<void>;
3737
unsubscribe?(): Promise<void>;
38-
complete?(argument: string, name: string): Promise<ResourceCompletion>;
38+
complete?<K extends keyof TArgs & string>(
39+
argumentName: K,
40+
value: TArgs[K],
41+
): Promise<ResourceCompletion>;
3942
}
4043

41-
export abstract class MCPResource implements ResourceProtocol {
44+
export abstract class MCPResource<TArgs extends Record<string, any> = {}>
45+
implements ResourceProtocol<TArgs>
46+
{
4247
abstract uri: string;
4348
abstract name: string;
4449
description?: string;

0 commit comments

Comments
 (0)