remove client class
This commit is contained in:
@@ -1,128 +0,0 @@
|
|||||||
import { Client } from "mcp-typescript/client/index.js";
|
|
||||||
import { SSEClientTransport } from "mcp-typescript/client/sse.js";
|
|
||||||
import { StdioClientTransport } from "mcp-typescript/client/stdio.js";
|
|
||||||
import {
|
|
||||||
ListResourcesResult,
|
|
||||||
ReadResourceResult,
|
|
||||||
ListResourcesResultSchema,
|
|
||||||
ReadResourceResultSchema,
|
|
||||||
ListPromptsResult,
|
|
||||||
ListPromptsResultSchema,
|
|
||||||
GetPromptResult,
|
|
||||||
GetPromptResultSchema,
|
|
||||||
ListToolsResult,
|
|
||||||
ListToolsResultSchema,
|
|
||||||
CallToolResult,
|
|
||||||
CallToolResultSchema,
|
|
||||||
GetPromptRequest,
|
|
||||||
ReadResourceRequest,
|
|
||||||
CallToolRequest,
|
|
||||||
} from "mcp-typescript/types.js";
|
|
||||||
|
|
||||||
export class McpClient {
|
|
||||||
private client: Client;
|
|
||||||
private transport?: SSEClientTransport | StdioClientTransport;
|
|
||||||
|
|
||||||
constructor(name: string, version: string) {
|
|
||||||
this.client = new Client({
|
|
||||||
name,
|
|
||||||
version,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async connect(url: string | URL) {
|
|
||||||
const urlObj = typeof url === "string" ? new URL(url) : url;
|
|
||||||
|
|
||||||
if (urlObj.protocol === "http:" || urlObj.protocol === "https:") {
|
|
||||||
this.transport = new SSEClientTransport();
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unsupported protocol: ${urlObj.protocol}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.transport.connect(urlObj);
|
|
||||||
await this.client.connect(this.transport);
|
|
||||||
}
|
|
||||||
|
|
||||||
async connectStdio(command: string, args: string[] = []) {
|
|
||||||
this.transport = new StdioClientTransport();
|
|
||||||
await this.transport.spawn({ command, args });
|
|
||||||
await this.client.connect(this.transport);
|
|
||||||
}
|
|
||||||
|
|
||||||
async close() {
|
|
||||||
await this.client.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resource Operations
|
|
||||||
async listResources(): Promise<ListResourcesResult> {
|
|
||||||
return await this.client.request(
|
|
||||||
{
|
|
||||||
method: "resources/list",
|
|
||||||
},
|
|
||||||
ListResourcesResultSchema,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async readResource(
|
|
||||||
params: ReadResourceRequest["params"],
|
|
||||||
): Promise<ReadResourceResult> {
|
|
||||||
return await this.client.request(
|
|
||||||
{
|
|
||||||
method: "resources/read",
|
|
||||||
params,
|
|
||||||
},
|
|
||||||
ReadResourceResultSchema,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prompt Operations
|
|
||||||
async listPrompts(): Promise<ListPromptsResult> {
|
|
||||||
return await this.client.request(
|
|
||||||
{
|
|
||||||
method: "prompts/list",
|
|
||||||
},
|
|
||||||
ListPromptsResultSchema,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getPrompt(
|
|
||||||
params: GetPromptRequest["params"],
|
|
||||||
): Promise<GetPromptResult> {
|
|
||||||
return await this.client.request(
|
|
||||||
{
|
|
||||||
method: "prompts/get",
|
|
||||||
params,
|
|
||||||
},
|
|
||||||
GetPromptResultSchema,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Tool Operations
|
|
||||||
async listTools(): Promise<ListToolsResult> {
|
|
||||||
return await this.client.request(
|
|
||||||
{
|
|
||||||
method: "tools/list",
|
|
||||||
},
|
|
||||||
ListToolsResultSchema,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async callTool(params: CallToolRequest["params"]): Promise<CallToolResult> {
|
|
||||||
return await this.client.request(
|
|
||||||
{
|
|
||||||
method: "tools/call",
|
|
||||||
params,
|
|
||||||
},
|
|
||||||
CallToolResultSchema,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
getServerCapabilities() {
|
|
||||||
return this.client.getServerCapabilities();
|
|
||||||
}
|
|
||||||
|
|
||||||
getServerVersion() {
|
|
||||||
return this.client.getServerVersion();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default McpClient;
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import McpClient from "./client.js";
|
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
|
|
||||||
import { Server } from "mcp-typescript/server/index.js";
|
import { Server } from "mcp-typescript/server/index.js";
|
||||||
@@ -11,7 +10,15 @@ import {
|
|||||||
ListResourcesRequestSchema,
|
ListResourcesRequestSchema,
|
||||||
ListToolsRequestSchema,
|
ListToolsRequestSchema,
|
||||||
ReadResourceRequestSchema,
|
ReadResourceRequestSchema,
|
||||||
|
ListResourcesResultSchema,
|
||||||
|
ReadResourceResultSchema,
|
||||||
|
ListPromptsResultSchema,
|
||||||
|
GetPromptResultSchema,
|
||||||
|
ListToolsResultSchema,
|
||||||
|
CallToolResultSchema,
|
||||||
} from "mcp-typescript/types.js";
|
} from "mcp-typescript/types.js";
|
||||||
|
import { Client } from "mcp-typescript/client/index.js";
|
||||||
|
import { StdioClientTransport } from "mcp-typescript/client/stdio.js";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
@@ -22,10 +29,12 @@ app.get("/sse", async (req, res) => {
|
|||||||
console.log("New SSE connection");
|
console.log("New SSE connection");
|
||||||
const command = decodeURIComponent(req.query.command as string);
|
const command = decodeURIComponent(req.query.command as string);
|
||||||
const args = decodeURIComponent(req.query.args as string).split(",");
|
const args = decodeURIComponent(req.query.args as string).split(",");
|
||||||
const mcpClient = new McpClient("MyApp", "1.0.0");
|
const mcpClient = new Client({ name: "MyApp", version: "1.0.0" });
|
||||||
await mcpClient.connectStdio(command, args);
|
const backingServerTransport = new StdioClientTransport();
|
||||||
|
await backingServerTransport.spawn({ command, args });
|
||||||
|
await mcpClient.connect(backingServerTransport);
|
||||||
|
|
||||||
const transport = new SSEServerTransport("/message");
|
const webAppTransport = new SSEServerTransport("/message");
|
||||||
const server = new Server({
|
const server = new Server({
|
||||||
name: "mcp-server-inspector",
|
name: "mcp-server-inspector",
|
||||||
version: "0.0.1",
|
version: "0.0.1",
|
||||||
@@ -38,31 +47,65 @@ app.get("/sse", async (req, res) => {
|
|||||||
await mcpClient.close();
|
await mcpClient.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
server.setRequestHandler(ListResourcesRequestSchema, () => {
|
server.setRequestHandler(ListResourcesRequestSchema, () =>
|
||||||
return mcpClient.listResources();
|
mcpClient.request(
|
||||||
});
|
{
|
||||||
|
method: "resources/list",
|
||||||
|
},
|
||||||
|
ListResourcesResultSchema,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
server.setRequestHandler(ReadResourceRequestSchema, (params) => {
|
server.setRequestHandler(ReadResourceRequestSchema, (params) =>
|
||||||
return mcpClient.readResource(params.params);
|
mcpClient.request(
|
||||||
});
|
{
|
||||||
|
method: "resources/read",
|
||||||
|
params: params.params,
|
||||||
|
},
|
||||||
|
ReadResourceResultSchema,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
server.setRequestHandler(ListPromptsRequestSchema, () => {
|
server.setRequestHandler(ListPromptsRequestSchema, () =>
|
||||||
return mcpClient.listPrompts();
|
mcpClient.request(
|
||||||
});
|
{
|
||||||
|
method: "prompts/list",
|
||||||
|
},
|
||||||
|
ListPromptsResultSchema,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
server.setRequestHandler(GetPromptRequestSchema, (params) => {
|
server.setRequestHandler(GetPromptRequestSchema, (params) => {
|
||||||
return mcpClient.getPrompt(params.params);
|
return mcpClient.request(
|
||||||
|
{
|
||||||
|
method: "prompts/get",
|
||||||
|
params: params.params,
|
||||||
|
},
|
||||||
|
GetPromptResultSchema,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.setRequestHandler(ListToolsRequestSchema, () => {
|
server.setRequestHandler(ListToolsRequestSchema, () =>
|
||||||
return mcpClient.listTools();
|
mcpClient.request(
|
||||||
});
|
{
|
||||||
|
method: "tools/list",
|
||||||
|
},
|
||||||
|
ListToolsResultSchema,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
server.setRequestHandler(CallToolRequestSchema, (params) => {
|
server.setRequestHandler(CallToolRequestSchema, (params) =>
|
||||||
return mcpClient.callTool(params.params);
|
mcpClient.request(
|
||||||
});
|
{
|
||||||
await transport.connectSSE(req, res);
|
method: "tools/call",
|
||||||
await server.connect(transport);
|
params: params.params,
|
||||||
|
},
|
||||||
|
CallToolResultSchema,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await webAppTransport.connectSSE(req, res);
|
||||||
|
await server.connect(webAppTransport);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/message", async (req, res) => {
|
app.post("/message", async (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user