Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience methods for issuing requests #35

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" });
}
}