-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
144 lines (127 loc) · 4.21 KB
/
server.ts
File metadata and controls
144 lines (127 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import cors from "cors";
import express, { type Request, type Response } from "express";
import fs from "node:fs/promises";
import path from "node:path";
import { z } from "zod";
import { RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
let DIST_DIR: string;
// If the server is running from the 'dist' folder (i.e., it's the compiled JS)
if (__dirname.endsWith(path.sep + 'dist')) {
DIST_DIR = __dirname;
} else {
// If the server is running from the project root (i.e., original TS)
DIST_DIR = path.join(__dirname, "dist");
}
const server = new McpServer({
name: "Wordly MCP App",
version: "1.0.0",
});
{
const resourceUri = "ui://wordly-mcp-app/mcp-app.html";
server.registerTool(
"visualize_rewrites",
{
title: "Visualize Rewrites",
description: "ALWAYS use this tool when the user asks to rewrite text in multiple styles, variations, or tones. Also use for grammar test, checks, rewrites, improvements, or paraphrasing requests. This tool provides an interactive visualization of text variations. Provide the original text and a list of rewritten variations grouped by intent (e.g., Formal, Casual, Professional, Enthusiastic, Concise, etc.).",
inputSchema: z.object({
original_text: z.string(),
variations: z.array(
z.object({
intent: z.string().describe("The goal of the rewrite (e.g. Fluency, Formal, Informal, Sensory)"),
text: z.string().describe("The rewritten text"),
})
),
}),
_meta: {
ui: {
resourceUri: resourceUri,
},
},
},
async (args: any): Promise<CallToolResult> => {
return {
content: [{ type: "text", text: JSON.stringify(args) }],
};
},
);
server.registerResource(
resourceUri,
resourceUri,
{},
async (): Promise<ReadResourceResult> => {
let html;
try {
html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8");
} catch (e) {
html = "<html><body><h1>App not built. Run npm run build.</h1></body></html>";
}
return {
contents: [
{
uri: resourceUri,
mimeType: RESOURCE_MIME_TYPE,
text: html,
_meta: {
ui: {
permissions: { clipboardWrite: {} },
},
},
},
],
};
},
);
}
if (process.argv.includes("--stdio")) {
const transport = new StdioServerTransport();
await server.connect(transport);
} else {
const app = express();
app.use(cors());
app.use(express.json());
app.post("/mcp", async (req: Request, res: Response) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
enableJsonResponse: true,
});
res.on("close", () => { transport.close(); });
await server.connect(transport);
await transport.handleRequest(req, res, req.body);
} catch (error) {
console.error("Error handling MCP request:", error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: "2.0",
error: { code: -32603, message: "Internal server error" },
id: null,
});
}
}
});
const httpServer = app.listen(PORT, (err) => {
if (err) {
console.error("Error starting server:", err);
process.exit(1);
}
console.log(`Server listening on http://localhost:${PORT}/mcp`);
});
function shutdown() {
console.log("\nShutting down...");
httpServer.close(() => {
console.log("Server closed");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}