Skip to content

Commit e95e91a

Browse files
Merge pull request #35 from modelcontextprotocol/justin/conveniences
Add convenience methods for issuing requests
2 parents 2b9b1b7 + 12c9234 commit e95e91a

File tree

2 files changed

+167
-2
lines changed

2 files changed

+167
-2
lines changed

src/client/index.ts

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Protocol } from "../shared/protocol.js";
1+
import { ProgressCallback, Protocol } from "../shared/protocol.js";
22
import { Transport } from "../shared/transport.js";
33
import {
44
ClientNotification,
@@ -11,6 +11,24 @@ import {
1111
Request,
1212
Result,
1313
ServerCapabilities,
14+
CompleteRequest,
15+
GetPromptRequest,
16+
ListPromptsRequest,
17+
ListResourcesRequest,
18+
ReadResourceRequest,
19+
SubscribeRequest,
20+
UnsubscribeRequest,
21+
CallToolRequest,
22+
ListToolsRequest,
23+
CompleteResultSchema,
24+
GetPromptResultSchema,
25+
ListPromptsResultSchema,
26+
ListResourcesResultSchema,
27+
ReadResourceResultSchema,
28+
CallToolResultSchema,
29+
ListToolsResultSchema,
30+
EmptyResultSchema,
31+
LoggingLevel,
1432
} from "../types.js";
1533

1634
/**
@@ -103,4 +121,106 @@ export class Client<
103121
getServerVersion(): Implementation | undefined {
104122
return this._serverVersion;
105123
}
124+
125+
async ping() {
126+
return this.request({ method: "ping" }, EmptyResultSchema);
127+
}
128+
129+
async complete(
130+
params: CompleteRequest["params"],
131+
onprogress?: ProgressCallback,
132+
) {
133+
return this.request(
134+
{ method: "completion/complete", params },
135+
CompleteResultSchema,
136+
onprogress,
137+
);
138+
}
139+
140+
async setLoggingLevel(level: LoggingLevel) {
141+
return this.request(
142+
{ method: "logging/setLevel", params: { level } },
143+
EmptyResultSchema,
144+
);
145+
}
146+
147+
async getPrompt(
148+
params: GetPromptRequest["params"],
149+
onprogress?: ProgressCallback,
150+
) {
151+
return this.request(
152+
{ method: "prompts/get", params },
153+
GetPromptResultSchema,
154+
onprogress,
155+
);
156+
}
157+
158+
async listPrompts(
159+
params?: ListPromptsRequest["params"],
160+
onprogress?: ProgressCallback,
161+
) {
162+
return this.request(
163+
{ method: "prompts/list", params },
164+
ListPromptsResultSchema,
165+
onprogress,
166+
);
167+
}
168+
169+
async listResources(
170+
params?: ListResourcesRequest["params"],
171+
onprogress?: ProgressCallback,
172+
) {
173+
return this.request(
174+
{ method: "resources/list", params },
175+
ListResourcesResultSchema,
176+
onprogress,
177+
);
178+
}
179+
180+
async readResource(
181+
params: ReadResourceRequest["params"],
182+
onprogress?: ProgressCallback,
183+
) {
184+
return this.request(
185+
{ method: "resources/read", params },
186+
ReadResourceResultSchema,
187+
onprogress,
188+
);
189+
}
190+
191+
async subscribeResource(params: SubscribeRequest["params"]) {
192+
return this.request(
193+
{ method: "resources/subscribe", params },
194+
EmptyResultSchema,
195+
);
196+
}
197+
198+
async unsubscribeResource(params: UnsubscribeRequest["params"]) {
199+
return this.request(
200+
{ method: "resources/unsubscribe", params },
201+
EmptyResultSchema,
202+
);
203+
}
204+
205+
async callTool(
206+
params: CallToolRequest["params"],
207+
onprogress?: ProgressCallback,
208+
) {
209+
return this.request(
210+
{ method: "tools/call", params },
211+
CallToolResultSchema,
212+
onprogress,
213+
);
214+
}
215+
216+
async listTools(
217+
params?: ListToolsRequest["params"],
218+
onprogress?: ProgressCallback,
219+
) {
220+
return this.request(
221+
{ method: "tools/list", params },
222+
ListToolsResultSchema,
223+
onprogress,
224+
);
225+
}
106226
}

src/server/index.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Protocol } from "../shared/protocol.js";
1+
import { ProgressCallback, Protocol } from "../shared/protocol.js";
22
import {
33
ClientCapabilities,
44
Implementation,
@@ -18,6 +18,11 @@ import {
1818
ListToolsRequestSchema,
1919
ListPromptsRequestSchema,
2020
SetLevelRequestSchema,
21+
CreateMessageRequest,
22+
CreateMessageResultSchema,
23+
EmptyResultSchema,
24+
LoggingMessageNotification,
25+
ResourceUpdatedNotification,
2126
} from "../types.js";
2227

2328
/**
@@ -133,4 +138,44 @@ export class Server<
133138
: undefined,
134139
};
135140
}
141+
142+
async ping() {
143+
return this.request({ method: "ping" }, EmptyResultSchema);
144+
}
145+
146+
async createMessage(
147+
params: CreateMessageRequest["params"],
148+
onprogress?: ProgressCallback,
149+
) {
150+
return this.request(
151+
{ method: "sampling/createMessage", params },
152+
CreateMessageResultSchema,
153+
onprogress,
154+
);
155+
}
156+
157+
async sendLoggingMessage(params: LoggingMessageNotification["params"]) {
158+
return this.notification({ method: "notifications/message", params });
159+
}
160+
161+
async sendResourceUpdated(params: ResourceUpdatedNotification["params"]) {
162+
return this.notification({
163+
method: "notifications/resources/updated",
164+
params,
165+
});
166+
}
167+
168+
async sendResourceListChanged() {
169+
return this.notification({
170+
method: "notifications/resources/list_changed",
171+
});
172+
}
173+
174+
async sendToolListChanged() {
175+
return this.notification({ method: "notifications/tools/list_changed" });
176+
}
177+
178+
async sendPromptListChanged() {
179+
return this.notification({ method: "notifications/prompts/list_changed" });
180+
}
136181
}

0 commit comments

Comments
 (0)