init server

This commit is contained in:
Ashwin Bhat
2024-10-07 16:27:49 -07:00
parent 1f8d44dafd
commit 70a9f96712
5 changed files with 925 additions and 4 deletions

74
server/src/client.ts Normal file
View File

@@ -0,0 +1,74 @@
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,
} 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(uri: string): Promise<ReadResourceResult> {
return await this.client.request(
{
method: "resources/read",
params: { uri },
},
ReadResourceResultSchema,
);
}
getServerCapabilities() {
return this.client.getServerCapabilities();
}
getServerVersion() {
return this.client.getServerVersion();
}
}
export default McpClient;

45
server/src/index.ts Normal file
View File

@@ -0,0 +1,45 @@
import McpClient from "./client.js";
import express from "express";
import http from "http";
import { WebSocket, WebSocketServer } from "ws";
const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
// Create and connect the MCP client
const mcpClient = new McpClient("MyApp", "1.0.0");
await mcpClient.connectStdio(
"/Users/ashwin/.nvm/versions/node/v18.20.4/bin/node",
["/Users/ashwin/code/example-servers/build/everything/index.js"],
);
wss.on("connection", (ws: WebSocket) => {
ws.on("message", async (message: string) => {
try {
const command = JSON.parse(message);
if (command.type === "listResources") {
const resources = await mcpClient.listResources();
ws.send(JSON.stringify({ type: "resources", data: resources }));
} else if (command.type === "readResource" && command.uri) {
const resource = await mcpClient.readResource(command.uri);
ws.send(JSON.stringify({ type: "resource", data: resource }));
}
} catch (error) {
console.error("Error:", error);
ws.send(JSON.stringify({ type: "error", message: String(error) }));
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Close the client when the server is shutting down
process.on("SIGINT", async () => {
await mcpClient.close();
process.exit();
});