-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (43 loc) · 1.62 KB
/
index.js
File metadata and controls
50 lines (43 loc) · 1.62 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
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import axios from "axios";
import * as cheerio from "cheerio";
import { z } from "zod";
// Initialize MCP server
const server = new McpServer({
name: "mdnlookup",
description: "A tool to fetch and summarize developer documentation from MDN.",
version: "1.0.0"
});
// Tool: Fetch documentation from MDN
server.tool(
"mdnlookup",
"Fetches and summarizes developer documentation from MDN based on a search query.",
{ query: z.string() },
async ({ query }) => {
try {
const searchUrl = `https://developer.mozilla.org/api/v1/search?q=${encodeURIComponent(query)}&locale=en-US`;
const searchRes = await axios.get(searchUrl);
const results = searchRes.data.documents;
if (!results.length) {
return { content: [{ type: "text", text: "No documentation found for this query." }] };
}
const docUrl = `https://developer.mozilla.org${results[0].mdn_url}`;
const docRes = await axios.get(docUrl);
const $ = cheerio.load(docRes.data);
// Extracting just the first paragraph for quick overview
const snippet = $('article p').first().text().trim();
return {
content: [{
type: "text",
text: `${snippet}\n\nMore info: ${docUrl}`
}]
};
} catch (error) {
return { content: [{ type: "text", text: `Error fetching docs: ${error.message}` }] };
}
}
);
// Start server via stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);