Skip to content

Commit

Permalink
Merge pull request #35 from modelcontextprotocol/justin/conveniences
Browse files Browse the repository at this point in the history
Add convenience methods for issuing requests
  • Loading branch information
jspahrsummers authored Nov 5, 2024
2 parents 2b9b1b7 + 12c9234 commit e95e91a
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 2 deletions.
122 changes: 121 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Protocol } from "../shared/protocol.js";
import { ProgressCallback, Protocol } from "../shared/protocol.js";
import { Transport } from "../shared/transport.js";
import {
ClientNotification,
Expand All @@ -11,6 +11,24 @@ import {
Request,
Result,
ServerCapabilities,
CompleteRequest,
GetPromptRequest,
ListPromptsRequest,
ListResourcesRequest,
ReadResourceRequest,
SubscribeRequest,
UnsubscribeRequest,
CallToolRequest,
ListToolsRequest,
CompleteResultSchema,
GetPromptResultSchema,
ListPromptsResultSchema,
ListResourcesResultSchema,
ReadResourceResultSchema,
CallToolResultSchema,
ListToolsResultSchema,
EmptyResultSchema,
LoggingLevel,
} from "../types.js";

/**
Expand Down Expand Up @@ -103,4 +121,106 @@ export class Client<
getServerVersion(): Implementation | undefined {
return this._serverVersion;
}

async ping() {
return this.request({ method: "ping" }, EmptyResultSchema);
}

async complete(
params: CompleteRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "completion/complete", params },
CompleteResultSchema,
onprogress,
);
}

async setLoggingLevel(level: LoggingLevel) {
return this.request(
{ method: "logging/setLevel", params: { level } },
EmptyResultSchema,
);
}

async getPrompt(
params: GetPromptRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "prompts/get", params },
GetPromptResultSchema,
onprogress,
);
}

async listPrompts(
params?: ListPromptsRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "prompts/list", params },
ListPromptsResultSchema,
onprogress,
);
}

async listResources(
params?: ListResourcesRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "resources/list", params },
ListResourcesResultSchema,
onprogress,
);
}

async readResource(
params: ReadResourceRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "resources/read", params },
ReadResourceResultSchema,
onprogress,
);
}

async subscribeResource(params: SubscribeRequest["params"]) {
return this.request(
{ method: "resources/subscribe", params },
EmptyResultSchema,
);
}

async unsubscribeResource(params: UnsubscribeRequest["params"]) {
return this.request(
{ method: "resources/unsubscribe", params },
EmptyResultSchema,
);
}

async callTool(
params: CallToolRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "tools/call", params },
CallToolResultSchema,
onprogress,
);
}

async listTools(
params?: ListToolsRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "tools/list", params },
ListToolsResultSchema,
onprogress,
);
}
}
47 changes: 46 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Protocol } from "../shared/protocol.js";
import { ProgressCallback, Protocol } from "../shared/protocol.js";
import {
ClientCapabilities,
Implementation,
Expand All @@ -18,6 +18,11 @@ import {
ListToolsRequestSchema,
ListPromptsRequestSchema,
SetLevelRequestSchema,
CreateMessageRequest,
CreateMessageResultSchema,
EmptyResultSchema,
LoggingMessageNotification,
ResourceUpdatedNotification,
} from "../types.js";

/**
Expand Down Expand Up @@ -133,4 +138,44 @@ export class Server<
: undefined,
};
}

async ping() {
return this.request({ method: "ping" }, EmptyResultSchema);
}

async createMessage(
params: CreateMessageRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "sampling/createMessage", params },
CreateMessageResultSchema,
onprogress,
);
}

async sendLoggingMessage(params: LoggingMessageNotification["params"]) {
return this.notification({ method: "notifications/message", params });
}

async sendResourceUpdated(params: ResourceUpdatedNotification["params"]) {
return this.notification({
method: "notifications/resources/updated",
params,
});
}

async sendResourceListChanged() {
return this.notification({
method: "notifications/resources/list_changed",
});
}

async sendToolListChanged() {
return this.notification({ method: "notifications/tools/list_changed" });
}

async sendPromptListChanged() {
return this.notification({ method: "notifications/prompts/list_changed" });
}
}

0 comments on commit e95e91a

Please sign in to comment.