Skip to content

Commit ae1963f

Browse files
authored
feat: Implements MCP client (#43)
Signed-off-by: John McBride <john@zuplo.com>
1 parent 46ab482 commit ae1963f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1531
-90
lines changed

examples/clients/ping/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# MCP Client Ping Example
2+
3+
This example demonstrates how to use the MCP client to connect to and ping an MCP server.
4+
5+
## Prerequisites
6+
7+
Make sure the calculator MCP server is running:
8+
9+
```bash
10+
cd ../calculator
11+
npm install
12+
npm run dev
13+
```
14+
15+
The server should be running on `http://localhost:3000`. Otherwise, use `BASE_URL` to set
16+
the URL to a different MCP server.
17+
18+
## Running the Example
19+
20+
```bash
21+
npm install
22+
npm run build
23+
npm start
24+
```

examples/clients/ping/package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/clients/ping/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@zuplo/mcp example ping client",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "tsc",
7+
"start": "node dist/index.js",
8+
"clean": "rm -rf ./dist/ && rm -rf ./node_modules/"
9+
},
10+
"dependencies": {
11+
"@zuplo/mcp": "file:../../../"
12+
}
13+
}

examples/clients/ping/src/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { MCPClient } from "@zuplo/mcp/client";
2+
import { ConsoleLogger } from "@zuplo/mcp/logger";
3+
import { HTTPClientTransport } from "@zuplo/mcp/transport/httpclient";
4+
5+
const logger = new ConsoleLogger();
6+
const client = new MCPClient({
7+
name: "Ping client",
8+
version: "0.0.0",
9+
logger,
10+
});
11+
12+
const url = process.env.BASE_URL || "http://localhost:3000/mcp"
13+
14+
try {
15+
const transport = new HTTPClientTransport({
16+
url,
17+
logger,
18+
});
19+
20+
await client.connect(transport);
21+
const initResult = await client.initialize();
22+
23+
logger.info("Connected successfully!", {
24+
serverInfo: initResult.serverInfo,
25+
protocolVersion: initResult.protocolVersion,
26+
});
27+
28+
logger.info("Attempting ping ...");
29+
await client.ping();
30+
logger.info("Ping successful!");
31+
} catch (error) {
32+
logger.error("Error running client example:", error);
33+
} finally {
34+
await client.disconnect();
35+
logger.info("Disconnected from server");
36+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "../../tsconfig.base.json",
2+
"extends": "../../../tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "./dist",
55
"rootDir": "./src"
@@ -8,6 +8,6 @@
88
"./src/**/*"
99
],
1010
"references": [
11-
{ "path": "../../" }
11+
{ "path": "../../../" }
1212
]
1313
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# MCP Client Tools Call Example
2+
3+
This example demonstrates how to use the MCP client to connect to and call tools.
4+
5+
## Prerequisites
6+
7+
Make sure the calculator server is running:
8+
9+
```bash
10+
cd ../calculator
11+
npm install
12+
npm run dev
13+
```
14+
15+
The server should be running on `http://localhost:3000`. Otherwise, use `BASE_URL` to set
16+
the URL to a different MCP server.
17+
18+
Note!!! This example expects a server with an "add" and "divide" tool that accepts 2 arguments: `a` and `b`.
19+
20+
## Running the Example
21+
22+
```bash
23+
npm install
24+
npm run build
25+
npm start
26+
```

examples/clients/tools-call/package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@zuplo/mcp example tool call client",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "tsc",
7+
"start": "node dist/index.js",
8+
"clean": "rm -rf ./dist/ && rm -rf ./node_modules/"
9+
},
10+
"dependencies": {
11+
"@zuplo/mcp": "file:../../../"
12+
}
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { MCPClient } from "@zuplo/mcp/client";
2+
import { ConsoleLogger } from "@zuplo/mcp/logger";
3+
import { HTTPClientTransport } from "@zuplo/mcp/transport/httpclient";
4+
5+
const logger = new ConsoleLogger();
6+
const client = new MCPClient({
7+
name: "Ping client",
8+
version: "0.0.0",
9+
logger,
10+
});
11+
12+
// This MCP client is intended to be tested against a calculator MCP server
13+
// with tools "add" and "divide".
14+
// The divide tool uses 0 as a denominator to simulating throwing an error.
15+
const url = process.env.BASE_URL || "http://localhost:3000/mcp"
16+
17+
try {
18+
// Create transport to connect to the calculator server
19+
const transport = new HTTPClientTransport({
20+
url,
21+
logger,
22+
});
23+
24+
await client.connect(transport);
25+
const initResult = await client.initialize();
26+
27+
logger.info("Connected successfully!", {
28+
serverInfo: initResult.serverInfo,
29+
protocolVersion: initResult.protocolVersion,
30+
});
31+
32+
const addResult = await client.callTool("add", { a: 5, b: 3 });
33+
logger.info("5 + 3 =", addResult);
34+
35+
// Test error handling - division by zero
36+
try {
37+
const divideResult = await client.callTool("divide", { a: 10, b: 0 });
38+
logger.info("10 ÷ 0 =", divideResult);
39+
} catch (error) {
40+
logger.info("Expected error for division by zero:", error);
41+
}
42+
} catch (error) {
43+
logger.error("Error running client example:", error);
44+
} finally {
45+
await client.disconnect();
46+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "../../tsconfig.base.json",
2+
"extends": "../../../tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "./dist",
55
"rootDir": "./src"
@@ -8,6 +8,6 @@
88
"./src/**/*"
99
],
1010
"references": [
11-
{ "path": "../../" }
11+
{ "path": "../../../" }
1212
]
1313
}

0 commit comments

Comments
 (0)