mcp-caller 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/index.js +200 -0
  4. package/package.json +37 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ab498
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # mcp-caller
2
+
3
+ `mcp-caller` is a small MCP client CLI that starts an MCP server, performs the protocol handshake, and calls a tool by JSON input.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ cd culled/mcp-caller
9
+ npm install
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ Call a stdio MCP server:
15
+
16
+ ```bash
17
+ node index.js \
18
+ --server-json '{"type":"stdio","command":"uvx","args":["--from","git+https://github.com/macsymwang/hello-mcp-server.git","hello-mcp-server"]}' \
19
+ --call-json '{"tool":"say_hello","params":{"name":"world"}}'
20
+ ```
21
+
22
+ Call a remote streamable HTTP MCP server:
23
+
24
+ ```bash
25
+ node index.js \
26
+ --server-json '{"mcpServers":{"context7":{"type":"streamable-http","url":"https://mcp.context7.com/mcp","headers":{"CONTEXT7_API_KEY":"YOUR_API_KEY"}}}}' \
27
+ --call-json '{"tool":"resolve-library-id","params":{"query":"Express","libraryName":"Express"}}'
28
+ ```
29
+
30
+ Read JSON from files instead of inline values:
31
+
32
+ ```bash
33
+ node index.js --server-file server.json --call-file call.json
34
+ ```
35
+
36
+ ## JSON Shapes
37
+
38
+ Server JSON:
39
+
40
+ ```json
41
+ {
42
+ "mcpServers": {
43
+ "name": {
44
+ "type": "stdio",
45
+ "command": "node",
46
+ "args": ["server.js"]
47
+ }
48
+ }
49
+ }
50
+ ```
51
+
52
+ Or a single server object:
53
+
54
+ ```json
55
+ {
56
+ "type": "streamable-http",
57
+ "url": "https://example.com/mcp",
58
+ "headers": {}
59
+ }
60
+ ```
61
+
62
+ Call JSON:
63
+
64
+ ```json
65
+ {
66
+ "tool": "tool-name",
67
+ "params": {}
68
+ }
69
+ ```
70
+
71
+ ## Notes
72
+
73
+ - The CLI prints text content when the tool result contains text.
74
+ - Use `--pretty` to format non-text JSON output.
75
+ - The Context7 integration example is covered by [test/cli.examples.test.js](d:/projects/culled/mcp-caller/test/cli.examples.test.js).
package/index.js ADDED
@@ -0,0 +1,200 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { readFileSync } from "node:fs";
4
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
5
+ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
6
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7
+
8
+ function printUsage() {
9
+ console.error(`Usage:
10
+ mcp-caller --server-json <json> --call-json <json>
11
+ mcp-caller --server-file <path> --call-file <path>
12
+
13
+ Server JSON shape:
14
+ { "mcpServers": { "name": { ... } } }
15
+ or a single server object with:
16
+ { "type": "streamable-http", "url": "...", "headers": { ... } }
17
+ { "type": "stdio", "command": "...", "args": [...], "cwd": "...", "env": { ... } }
18
+
19
+ Call JSON shape:
20
+ { "tool": "tool-name", "params": { ... } }
21
+ `);
22
+ }
23
+
24
+ function parseJson(raw, label) {
25
+ try {
26
+ const trimmed = raw.trim();
27
+ const candidates = [
28
+ trimmed,
29
+ (trimmed.startsWith("'") && trimmed.endsWith("'")) || (trimmed.startsWith('"') && trimmed.endsWith('"'))
30
+ ? trimmed.slice(1, -1)
31
+ : trimmed,
32
+ trimmed.replace(/\\"/g, '"')
33
+ ];
34
+
35
+ let lastError;
36
+ for (const candidate of candidates) {
37
+ try {
38
+ return JSON.parse(candidate);
39
+ } catch (error) {
40
+ lastError = error;
41
+ }
42
+ }
43
+
44
+ throw lastError ?? new Error("Unable to parse JSON");
45
+ } catch (error) {
46
+ throw new Error(`Invalid ${label}: ${error instanceof Error ? error.message : String(error)}`);
47
+ }
48
+ }
49
+
50
+ function readSource(options, jsonKey, fileKey, label) {
51
+ if (options[fileKey]) {
52
+ return parseJson(readFileSync(options[fileKey], "utf8"), label);
53
+ }
54
+ if (options[jsonKey]) {
55
+ return parseJson(options[jsonKey], label);
56
+ }
57
+ throw new Error(`Missing ${label}`);
58
+ }
59
+
60
+ function parseArgs(argv) {
61
+ const options = {};
62
+ for (let i = 0; i < argv.length; i += 1) {
63
+ const token = argv[i];
64
+ if (token === "--help" || token === "-h") {
65
+ options.help = true;
66
+ continue;
67
+ }
68
+ if (token === "--server-json") {
69
+ options.serverJson = argv[++i];
70
+ continue;
71
+ }
72
+ if (token === "--server-file") {
73
+ options.serverFile = argv[++i];
74
+ continue;
75
+ }
76
+ if (token === "--call-json") {
77
+ options.callJson = argv[++i];
78
+ continue;
79
+ }
80
+ if (token === "--call-file") {
81
+ options.callFile = argv[++i];
82
+ continue;
83
+ }
84
+ if (token === "--server-name") {
85
+ options.serverName = argv[++i];
86
+ continue;
87
+ }
88
+ if (token === "--pretty") {
89
+ options.pretty = true;
90
+ continue;
91
+ }
92
+ throw new Error(`Unknown option: ${token}`);
93
+ }
94
+ return options;
95
+ }
96
+
97
+ function resolveServerDefinition(serverSource, serverName) {
98
+ if (serverSource?.mcpServers && typeof serverSource.mcpServers === "object") {
99
+ const entries = Object.entries(serverSource.mcpServers);
100
+ if (entries.length === 0) {
101
+ throw new Error("server JSON contains an empty mcpServers object");
102
+ }
103
+ const selectedName = serverName ?? entries[0][0];
104
+ const selectedServer = serverSource.mcpServers[selectedName];
105
+ if (!selectedServer) {
106
+ throw new Error(`Server "${selectedName}" was not found in mcpServers`);
107
+ }
108
+ return { name: selectedName, server: selectedServer };
109
+ }
110
+
111
+ return { name: serverName ?? "server", server: serverSource };
112
+ }
113
+
114
+ function createTransport(server) {
115
+ if (!server || typeof server !== "object") {
116
+ throw new Error("Server definition must be an object");
117
+ }
118
+
119
+ if (server.type === "streamable-http") {
120
+ if (!server.url) {
121
+ throw new Error("streamable-http server is missing url");
122
+ }
123
+ return new StreamableHTTPClientTransport(new URL(server.url), {
124
+ requestInit: {
125
+ headers: server.headers ?? {}
126
+ }
127
+ });
128
+ }
129
+
130
+ if (server.type === "stdio") {
131
+ if (!server.command) {
132
+ throw new Error("stdio server is missing command");
133
+ }
134
+ return new StdioClientTransport({
135
+ command: server.command,
136
+ args: Array.isArray(server.args) ? server.args : [],
137
+ cwd: server.cwd,
138
+ env: server.env ?? {},
139
+ stderr: "inherit"
140
+ });
141
+ }
142
+
143
+ throw new Error(`Unsupported server type: ${server.type}`);
144
+ }
145
+
146
+ function extractText(result) {
147
+ const pieces = [];
148
+ for (const item of result?.content ?? []) {
149
+ if (item?.type === "text" && typeof item.text === "string") {
150
+ pieces.push(item.text);
151
+ }
152
+ }
153
+ return pieces.join("\n");
154
+ }
155
+
156
+ async function main() {
157
+ const options = parseArgs(process.argv.slice(2));
158
+ if (options.help) {
159
+ printUsage();
160
+ return;
161
+ }
162
+
163
+ const serverSource = readSource(options, "serverJson", "serverFile", "--server-json/--server-file");
164
+ const callSource = readSource(options, "callJson", "callFile", "--call-json/--call-file");
165
+
166
+ if (!callSource || typeof callSource !== "object") {
167
+ throw new Error("Call JSON must be an object");
168
+ }
169
+ if (typeof callSource.tool !== "string" || !callSource.tool) {
170
+ throw new Error('Call JSON must include a non-empty "tool" string');
171
+ }
172
+
173
+ const { server } = resolveServerDefinition(serverSource, options.serverName);
174
+ const transport = createTransport(server);
175
+ const client = new Client({ name: "mcp-caller", version: "0.0.1" }, { capabilities: {} });
176
+
177
+ try {
178
+ await client.connect(transport);
179
+
180
+ const result = await client.callTool({
181
+ name: callSource.tool,
182
+ arguments: callSource.params ?? callSource.arguments ?? {}
183
+ });
184
+
185
+ const text = extractText(result);
186
+ if (text) {
187
+ console.log(text);
188
+ return;
189
+ }
190
+
191
+ console.log(JSON.stringify(result, null, options.pretty ? 2 : 0));
192
+ } finally {
193
+ await client.close().catch(() => {});
194
+ }
195
+ }
196
+
197
+ main().catch((error) => {
198
+ console.error(error instanceof Error ? error.stack ?? error.message : String(error));
199
+ process.exitCode = 1;
200
+ });
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "mcp-caller",
3
+ "version": "0.0.1",
4
+ "description": "A small MCP client CLI that can start a server and call a tool from JSON input.",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "bin": "./index.js",
8
+ "files": [
9
+ "index.js",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "scripts": {
14
+ "start": "node index.js",
15
+ "test": "vitest run",
16
+ "test:watch": "vitest",
17
+ "test:context7": "vitest run test/cli.examples.test.js -t \"Context7 resolve-library-id\""
18
+ },
19
+ "dependencies": {
20
+ "@modelcontextprotocol/sdk": "^1.9.0",
21
+ "zod": "^3.23.8"
22
+ },
23
+ "devDependencies": {
24
+ "vitest": "^4.1.4"
25
+ },
26
+ "keywords": [
27
+ "mcp",
28
+ "cli",
29
+ "stdio",
30
+ "streamable-http"
31
+ ],
32
+ "author": "ab498",
33
+ "license": "MIT",
34
+ "engines": {
35
+ "node": ">=18"
36
+ }
37
+ }