@reus-able/frontend-helper-mcp 1.0.1 → 1.0.2
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/index.js +73 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
3
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
-
import { ListPromptsRequestSchema, GetPromptRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { ListPromptsRequestSchema, GetPromptRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
5
5
|
import fs from 'fs/promises';
|
|
6
6
|
import path from 'path';
|
|
7
7
|
import { fileURLToPath } from 'url';
|
|
@@ -15,20 +15,36 @@ async function createServer(promptsDir) {
|
|
|
15
15
|
}, {
|
|
16
16
|
capabilities: {
|
|
17
17
|
prompts: {},
|
|
18
|
+
resources: {},
|
|
18
19
|
},
|
|
19
20
|
});
|
|
20
|
-
|
|
21
|
+
// Helper functions
|
|
22
|
+
async function getValidPromptFiles() {
|
|
21
23
|
try {
|
|
22
|
-
|
|
23
|
-
try {
|
|
24
|
-
await fs.access(promptsDir);
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
console.error(`Directory ${promptsDir} does not exist.`);
|
|
28
|
-
return { prompts: [] };
|
|
29
|
-
}
|
|
24
|
+
await fs.access(promptsDir);
|
|
30
25
|
const files = await fs.readdir(promptsDir);
|
|
31
|
-
|
|
26
|
+
return files.filter((f) => ['.txt', '.md', '.json'].includes(path.extname(f).toLowerCase()));
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error(`Error accessing prompts directory ${promptsDir}:`, error);
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function findPromptFileByName(name) {
|
|
34
|
+
const files = await getValidPromptFiles();
|
|
35
|
+
return files.find((f) => path.parse(f).name === name);
|
|
36
|
+
}
|
|
37
|
+
function getMimeType(filename) {
|
|
38
|
+
const ext = path.extname(filename).toLowerCase();
|
|
39
|
+
if (ext === '.md')
|
|
40
|
+
return 'text/markdown';
|
|
41
|
+
if (ext === '.json')
|
|
42
|
+
return 'application/json';
|
|
43
|
+
return 'text/plain';
|
|
44
|
+
}
|
|
45
|
+
mcpServer.server.setRequestHandler(ListPromptsRequestSchema, async () => {
|
|
46
|
+
try {
|
|
47
|
+
const promptFiles = await getValidPromptFiles();
|
|
32
48
|
const prompts = promptFiles.map((file) => {
|
|
33
49
|
const name = path.parse(file).name;
|
|
34
50
|
return {
|
|
@@ -46,9 +62,7 @@ async function createServer(promptsDir) {
|
|
|
46
62
|
mcpServer.server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
47
63
|
const promptName = request.params.name;
|
|
48
64
|
try {
|
|
49
|
-
const
|
|
50
|
-
const file = files.find((f) => path.parse(f).name === promptName &&
|
|
51
|
-
['.txt', '.md', '.json'].includes(path.extname(f).toLowerCase()));
|
|
65
|
+
const file = await findPromptFileByName(promptName);
|
|
52
66
|
if (!file) {
|
|
53
67
|
throw new Error(`Prompt not found: ${promptName}`);
|
|
54
68
|
}
|
|
@@ -69,6 +83,51 @@ async function createServer(promptsDir) {
|
|
|
69
83
|
throw new Error(`Failed to load prompt ${promptName}: ${error}`);
|
|
70
84
|
}
|
|
71
85
|
});
|
|
86
|
+
mcpServer.server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
87
|
+
try {
|
|
88
|
+
const promptFiles = await getValidPromptFiles();
|
|
89
|
+
const resources = promptFiles.map((file) => {
|
|
90
|
+
const name = path.parse(file).name;
|
|
91
|
+
const mimeType = getMimeType(file);
|
|
92
|
+
return {
|
|
93
|
+
uri: `prompt://${name}`,
|
|
94
|
+
name: name,
|
|
95
|
+
mimeType: mimeType,
|
|
96
|
+
description: `Content of ${file}`,
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
return { resources };
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error('Error listing resources:', error);
|
|
103
|
+
return { resources: [] };
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
mcpServer.server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
107
|
+
const uri = request.params.uri;
|
|
108
|
+
// Handle prompt://name format
|
|
109
|
+
const name = uri.replace(/^prompt:\/\//, '');
|
|
110
|
+
try {
|
|
111
|
+
const file = await findPromptFileByName(name);
|
|
112
|
+
if (!file) {
|
|
113
|
+
throw new Error(`Resource not found: ${uri}`);
|
|
114
|
+
}
|
|
115
|
+
const content = await fs.readFile(path.join(promptsDir, file), 'utf-8');
|
|
116
|
+
const mimeType = getMimeType(file);
|
|
117
|
+
return {
|
|
118
|
+
contents: [
|
|
119
|
+
{
|
|
120
|
+
uri: uri,
|
|
121
|
+
mimeType: mimeType,
|
|
122
|
+
text: content,
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
throw new Error(`Failed to read resource ${uri}: ${error}`);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
72
131
|
return mcpServer;
|
|
73
132
|
}
|
|
74
133
|
async function main() {
|