diff --git a/examples/clients/sse/.gitignore b/examples/clients/sse/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/examples/clients/sse/.gitignore @@ -0,0 +1 @@ +.env diff --git a/examples/clients/sse/README.md b/examples/clients/sse/README.md new file mode 100644 index 0000000..68f61ae --- /dev/null +++ b/examples/clients/sse/README.md @@ -0,0 +1,22 @@ +# MCP Client with SSE stream servers + +This example demonstrates how to use the MCP client to connect to and work with +servers that serve their content in `data: ` streams (i.e., SSE). +This calls the GitHub MCP server `get_me` tool to demonstrate this. + +## Running the Example + +```bash +npm install +npm run build + +# Provide a GitHub token in the env for the Auth header +# to the GitHub MCP server +AUTH_TOKEN="ghp_..." npm start +``` + +## Supported env + +```env +AUTH_TOKEN="ghp_..." +``` diff --git a/examples/clients/sse/package-lock.json b/examples/clients/sse/package-lock.json new file mode 100644 index 0000000..2c9f1d5 --- /dev/null +++ b/examples/clients/sse/package-lock.json @@ -0,0 +1,35 @@ +{ + "name": "@zuplo/mcp-example-client-sse", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@zuplo/mcp-example-client-sse", + "dependencies": { + "@zuplo/mcp": "file:../../../" + } + }, + "../../..": { + "name": "@zuplo/mcp", + "version": "0.0.30", + "license": "MIT", + "dependencies": { + "eventsource-parser": "^3.0.6", + "zod": "^3.25.0" + }, + "devDependencies": { + "@types/jest": "^29.5.14", + "@types/node": "^22.15.14", + "jest": "^29.7.0", + "prettier": "^3.6.2", + "ts-jest": "^29.3.2", + "ts-node": "^10.9.2", + "typescript": "^5.8.3" + } + }, + "node_modules/@zuplo/mcp": { + "resolved": "../../..", + "link": true + } + } +} diff --git a/examples/clients/sse/package.json b/examples/clients/sse/package.json new file mode 100644 index 0000000..8779613 --- /dev/null +++ b/examples/clients/sse/package.json @@ -0,0 +1,13 @@ +{ + "name": "@zuplo/mcp-example-client-sse", + "private": true, + "type": "module", + "scripts": { + "build": "tsc", + "start": "node dist/index.js", + "clean": "rm -rf ./dist/ && rm -rf ./node_modules/" + }, + "dependencies": { + "@zuplo/mcp": "file:../../../" + } +} diff --git a/examples/clients/sse/src/index.ts b/examples/clients/sse/src/index.ts new file mode 100644 index 0000000..17dd567 --- /dev/null +++ b/examples/clients/sse/src/index.ts @@ -0,0 +1,44 @@ +import { MCPClient } from "@zuplo/mcp/client"; +import { ConsoleLogger } from "@zuplo/mcp/logger"; +import { HTTPClientTransport } from "@zuplo/mcp/transport/httpclient"; + +const logger = new ConsoleLogger(); +const client = new MCPClient({ + name: "GitHub SSE client", + version: "0.0.0", + logger, +}); + +const url = "https://api.githubcopilot.com/mcp/"; +const authToken = process.env.AUTH_TOKEN; + +if (!authToken) { + throw Error( + "no AUTH_TOKEN env var provided. Please provide a GitHub PAT for use with the GitHub MCP server." + ); +} + +try { + const transport = new HTTPClientTransport({ + url, + logger, + headers: { + Authorization: `Bearer ${authToken}`, + }, + }); + + await client.connect(transport); + const initResult = await client.initialize(); + + logger.info("Connected successfully!", { + serverInfo: initResult.serverInfo, + protocolVersion: initResult.protocolVersion, + }); + + const userResult = await client.callTool("get_me", {}); + logger.info("GitHub user:", userResult); +} catch (error) { + logger.error("Error running client example:", error); +} finally { + await client.disconnect(); +} diff --git a/examples/clients/sse/tsconfig.json b/examples/clients/sse/tsconfig.json new file mode 100644 index 0000000..eb3c717 --- /dev/null +++ b/examples/clients/sse/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./src" + }, + "include": ["./src/**/*"], + "references": [{ "path": "../../../" }] +}