cli-mcp-mapper 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/index.js +142 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steffen Blake
|
|
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 @@
|
|
|
1
|
+
# cli-mcp-mapper
|
package/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
import { spawn } from "child_process";
|
|
7
|
+
import { readFile } from "fs/promises";
|
|
8
|
+
import { homedir } from "os";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
|
|
11
|
+
// Load config from env variable or default path
|
|
12
|
+
const defaultConfig = join(homedir(), ".config", "cli-mcp-mapper", "commands.json");
|
|
13
|
+
const configPath = process.env.CLI_MCP_MAPPER_CONFIG || defaultConfig;
|
|
14
|
+
const config = JSON.parse(await readFile(configPath, "utf-8"));
|
|
15
|
+
|
|
16
|
+
const server = new Server(
|
|
17
|
+
{
|
|
18
|
+
name: "cli-mcp-mapper",
|
|
19
|
+
version: "1.0.0",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
capabilities: {
|
|
23
|
+
tools: Object.keys(config.commands).reduce((acc, name) => {
|
|
24
|
+
acc[name] = true;
|
|
25
|
+
return acc;
|
|
26
|
+
}, {}),
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// List available tools
|
|
32
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
33
|
+
const tools = Object.entries(config.commands).map(([name, cmd]) => ({
|
|
34
|
+
name,
|
|
35
|
+
description: cmd.description,
|
|
36
|
+
inputSchema: buildInputSchema(cmd.parameters),
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
return { tools };
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Execute tool
|
|
43
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
44
|
+
const { name, arguments: args } = request.params;
|
|
45
|
+
const commandConfig = config.commands[name];
|
|
46
|
+
|
|
47
|
+
if (!commandConfig) {
|
|
48
|
+
throw new Error(`Unknown command: ${name}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const cmdArray = buildCommand(commandConfig, args);
|
|
52
|
+
const result = await executeCommand(cmdArray);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
content: [
|
|
56
|
+
{
|
|
57
|
+
type: "text",
|
|
58
|
+
text: result,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
function buildInputSchema(parameters) {
|
|
65
|
+
const properties = {};
|
|
66
|
+
const required = [];
|
|
67
|
+
|
|
68
|
+
for (const [name, param] of Object.entries(parameters || {})) {
|
|
69
|
+
properties[name] = {
|
|
70
|
+
type: param.type,
|
|
71
|
+
description: param.description,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
if (param.enum) properties[name].enum = param.enum;
|
|
75
|
+
if (param.default) properties[name].default = param.default;
|
|
76
|
+
if (param.required) required.push(name);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties,
|
|
82
|
+
required,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function buildCommand(commandConfig, args) {
|
|
87
|
+
const cmd = [commandConfig.command, ...(commandConfig.baseArgs || [])];
|
|
88
|
+
|
|
89
|
+
// Add positional args first
|
|
90
|
+
const positional = Object.entries(commandConfig.parameters || {})
|
|
91
|
+
.filter(([_, param]) => param.position !== undefined)
|
|
92
|
+
.sort(([_, a], [__, b]) => a.position - b.position);
|
|
93
|
+
|
|
94
|
+
for (const [name, param] of positional) {
|
|
95
|
+
if (args[name] !== undefined) {
|
|
96
|
+
cmd.push(String(args[name]));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Add named args
|
|
101
|
+
for (const [name, param] of Object.entries(commandConfig.parameters || {})) {
|
|
102
|
+
if (param.position !== undefined) continue; // Skip positional
|
|
103
|
+
if (args[name] === undefined) continue; // Skip if not provided
|
|
104
|
+
|
|
105
|
+
if (param.type === "boolean") {
|
|
106
|
+
if (args[name] === true) {
|
|
107
|
+
cmd.push(param.argName);
|
|
108
|
+
if (param.argValue) cmd.push(param.argValue);
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
cmd.push(param.argName);
|
|
112
|
+
cmd.push(String(args[name]));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return cmd;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function executeCommand(cmdArray) {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
const [command, ...args] = cmdArray;
|
|
122
|
+
const proc = spawn(command, args, { shell: true });
|
|
123
|
+
|
|
124
|
+
let stdout = "";
|
|
125
|
+
let stderr = "";
|
|
126
|
+
|
|
127
|
+
proc.stdout.on("data", (data) => (stdout += data));
|
|
128
|
+
proc.stderr.on("data", (data) => (stderr += data));
|
|
129
|
+
|
|
130
|
+
proc.on("close", (code) => {
|
|
131
|
+
if (code !== 0) {
|
|
132
|
+
reject(new Error(`Command failed with code ${code}\n${stderr}`));
|
|
133
|
+
} else {
|
|
134
|
+
resolve(stdout || stderr);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Start server
|
|
141
|
+
const transport = new StdioServerTransport();
|
|
142
|
+
await server.connect(transport);
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cli-mcp-mapper",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "mcp server for mapping cli commands to mcp tools",
|
|
5
|
+
"homepage": "https://github.com/SteffenBlake/cli-mcp-mapper#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/SteffenBlake/cli-mcp-mapper/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/SteffenBlake/cli-mcp-mapper.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "SteffenBlake",
|
|
15
|
+
"bin": {
|
|
16
|
+
"cli-mcp-mapper": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@modelcontextprotocol/sdk": "^1.25.3"
|
|
21
|
+
}
|
|
22
|
+
}
|