@gluip/chart-canvas-mcp 0.1.2 → 0.1.3
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/dist/api.js +25 -2
- package/dist/index.js +8 -6
- package/package.json +1 -1
package/dist/api.js
CHANGED
|
@@ -3,11 +3,33 @@ import cors from "cors";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
5
5
|
import { stateManager } from "./state.js";
|
|
6
|
+
import { createServer } from "http";
|
|
6
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
7
8
|
const __dirname = path.dirname(__filename);
|
|
8
|
-
|
|
9
|
+
let serverPort = null;
|
|
10
|
+
export function getServerPort() {
|
|
11
|
+
return serverPort;
|
|
12
|
+
}
|
|
13
|
+
async function findAvailablePort(startPort) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const server = createServer();
|
|
16
|
+
server.listen(startPort, () => {
|
|
17
|
+
const port = server.address().port;
|
|
18
|
+
server.close(() => resolve(port));
|
|
19
|
+
});
|
|
20
|
+
server.on("error", (err) => {
|
|
21
|
+
if (err.code === "EADDRINUSE") {
|
|
22
|
+
resolve(findAvailablePort(startPort + 1));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
reject(err);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export async function startApiServer() {
|
|
9
31
|
const app = express();
|
|
10
|
-
const PORT = 3000;
|
|
32
|
+
const PORT = await findAvailablePort(3000);
|
|
11
33
|
app.use(cors());
|
|
12
34
|
app.use(express.json());
|
|
13
35
|
// API endpoints
|
|
@@ -25,6 +47,7 @@ export function startApiServer() {
|
|
|
25
47
|
res.sendFile(path.join(frontendDistPath, "index.html"));
|
|
26
48
|
});
|
|
27
49
|
app.listen(PORT, () => {
|
|
50
|
+
serverPort = PORT;
|
|
28
51
|
console.error(`Server running on http://localhost:${PORT}`);
|
|
29
52
|
console.error(`- API: http://localhost:${PORT}/state`);
|
|
30
53
|
console.error(`- Frontend: http://localhost:${PORT}`);
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
|
3
3
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
4
|
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
5
5
|
import { stateManager } from "./state.js";
|
|
6
|
-
import { startApiServer } from "./api.js";
|
|
6
|
+
import { startApiServer, getServerPort } from "./api.js";
|
|
7
7
|
import open from "open";
|
|
8
8
|
const server = new Server({
|
|
9
9
|
name: "chart-canvas-server",
|
|
@@ -140,11 +140,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
140
140
|
description,
|
|
141
141
|
xLabels,
|
|
142
142
|
});
|
|
143
|
+
const port = getServerPort() || 3000;
|
|
143
144
|
return {
|
|
144
145
|
content: [
|
|
145
146
|
{
|
|
146
147
|
type: "text",
|
|
147
|
-
text: `Added ${type} chart with ID ${viz.id}. View it at http://localhost
|
|
148
|
+
text: `Added ${type} chart with ID ${viz.id}. View it at http://localhost:${port}`,
|
|
148
149
|
},
|
|
149
150
|
],
|
|
150
151
|
};
|
|
@@ -176,12 +177,14 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
176
177
|
}
|
|
177
178
|
case "showCanvas": {
|
|
178
179
|
try {
|
|
179
|
-
|
|
180
|
+
const port = getServerPort() || 3000;
|
|
181
|
+
const url = `http://localhost:${port}`;
|
|
182
|
+
await open(url);
|
|
180
183
|
return {
|
|
181
184
|
content: [
|
|
182
185
|
{
|
|
183
186
|
type: "text",
|
|
184
|
-
text:
|
|
187
|
+
text: `Opened canvas in browser at ${url}`,
|
|
185
188
|
},
|
|
186
189
|
],
|
|
187
190
|
};
|
|
@@ -203,12 +206,11 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
203
206
|
});
|
|
204
207
|
async function main() {
|
|
205
208
|
// Start the API server for frontend
|
|
206
|
-
startApiServer();
|
|
209
|
+
await startApiServer();
|
|
207
210
|
// Start MCP server on stdio
|
|
208
211
|
const transport = new StdioServerTransport();
|
|
209
212
|
await server.connect(transport);
|
|
210
213
|
console.error("Chart Canvas MCP Server running");
|
|
211
|
-
console.error("API server running on http://localhost:3000");
|
|
212
214
|
}
|
|
213
215
|
main().catch((error) => {
|
|
214
216
|
console.error("Fatal error:", error);
|
package/package.json
CHANGED