|
| 1 | +#!/usr/bin/env node |
| 2 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 3 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 4 | +import { |
| 5 | + CallToolRequestSchema, |
| 6 | + ListToolsRequestSchema, |
| 7 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 8 | +import { z } from 'zod'; |
| 9 | +import { zodToJsonSchema } from 'zod-to-json-schema'; |
| 10 | + |
| 11 | +import { fetchData } from './utils.js'; |
| 12 | +import { VERSION } from './version.js'; |
| 13 | + |
| 14 | +const server = new Server( |
| 15 | + { |
| 16 | + name: "open-digger-mcp-server", |
| 17 | + version: VERSION, |
| 18 | + }, |
| 19 | + { |
| 20 | + capabilities: { |
| 21 | + tools: {}, |
| 22 | + }, |
| 23 | + } |
| 24 | +); |
| 25 | + |
| 26 | +const BASE_URL = 'https://oss.open-digger.cn/'; |
| 27 | + |
| 28 | +const inputSchema = z.object({ |
| 29 | + platform: z.enum(['GitHub', 'Gitee']).describe('Platform of the repo or user (GitHub, Gitee).'), |
| 30 | + entityType: z.enum(['Repo', 'User']).describe('What is the entity of the metric (Repo, User).'), |
| 31 | + owner: z.string().optional().describe('The owner name of the repo to get a metric data.'), |
| 32 | + repo: z.string().optional().describe('The repo name of the repo to get a metric data.'), |
| 33 | + login: z.string().optional().describe('The user login to get a metric data of a user.'), |
| 34 | + metricName: z.enum(['openrank', 'community_openrank', 'activity']).describe('The metric name to get the data.'), |
| 35 | +}); |
| 36 | + |
| 37 | +server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 38 | + return { |
| 39 | + tools: [ |
| 40 | + { |
| 41 | + name: "get_open_digger_metric", |
| 42 | + description: "Get metric data of OpenDigger", |
| 43 | + inputSchema: zodToJsonSchema(inputSchema), |
| 44 | + }, |
| 45 | + ], |
| 46 | + }; |
| 47 | +}); |
| 48 | + |
| 49 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 50 | + try { |
| 51 | + if (!request.params.arguments) { |
| 52 | + throw new Error("Arguments are required"); |
| 53 | + } |
| 54 | + |
| 55 | + switch (request.params.name) { |
| 56 | + case 'get_open_digger_metric': { |
| 57 | + const args = inputSchema.parse(request.params.arguments); |
| 58 | + const platform = args.platform.toString().toLowerCase(); |
| 59 | + let url = ''; |
| 60 | + if (args.entityType === 'Repo') { |
| 61 | + url = `${BASE_URL}${platform}/${args.owner}/${args.repo}/${args.metricName}.json`; |
| 62 | + } else { |
| 63 | + url = `${BASE_URL}${platform}/${args.login}/${args.metricName}.json`; |
| 64 | + } |
| 65 | + |
| 66 | + const data = await fetchData(url); |
| 67 | + return { |
| 68 | + content: [{ type: 'text', text: JSON.stringify(data, null, 2) }], |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + default: |
| 73 | + throw new Error(`Unknown tool: ${request.params.name}`); |
| 74 | + } |
| 75 | + } catch (e) { |
| 76 | + if (e instanceof z.ZodError) { |
| 77 | + throw new Error(`Invalid input: ${JSON.stringify(e.errors)}`); |
| 78 | + } |
| 79 | + throw e; |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +async function main() { |
| 84 | + const transport = new StdioServerTransport(); |
| 85 | + await server.connect(transport); |
| 86 | + console.error("OpenDigger MCP Server running on stdio"); |
| 87 | +} |
| 88 | + |
| 89 | +main().catch((error) => { |
| 90 | + console.error("Fatal error in main():", error); |
| 91 | + process.exit(1); |
| 92 | +}); |
0 commit comments