返回市场
MCP-js-服务器

MCP-js-服务器

作者:davlgd5 星标更新:2025-08-05

项目介绍

MCP Server - JavaScript SDK

这是一个非官方的Model Context Protocol JavaScript SDK。

使用方法

将包导入到你的项目中:

npm install mcp-js-server

创建文件来定义服务器的提示、资源和工具。参考mcp-clever-demo,或下面的例子:

提示

// prompts.js
export const prompts = {
    hello_world: {
        description: '一个简单的提示,说你好。',
        arguments: [],
        messages: [{
            role: 'assistant',
            content: {
                type: 'text',
                text: '你好,世界!'
            }
        }]
    }
};

资源

// resources.js
export const resources = {
    apiReference: {
        uri: 'https://api.example.com/openapi.json',
        mimeType: 'application/json'
    }
};

工具

// tools.js
export const tools = {
    simple_tool: {
        description: '一个简单的工具',
        handler: async () => new Date().toLocaleString(),
        schema: {
            type: 'object',
            properties: {},
            required: []
        }
    },
    complex_tool: {
        description: '一个复杂的工具',
        handler: async ({ param1, param2 }) => {
            return `param1: ${param1}, param2: ${param2}`;
        },
        schema: {
            type: 'object',
            properties: {
                param1: { type: 'string' },
                param2: { type:  'string'}
            },
            required: ['param1', 'param2']
        }
    }
};

服务器

然后使用以下代码创建一个服务器实例:

// server.js
import { MCP } from 'mcp-server';
import { tools } from './tools.js';
import { prompts } from './prompts.js';
import { resources } from './resources.js';

const infos = {
    name: 'mcp-demo-server',
    version: '0.1.0'
};

const server = new MCP(infos, prompts, resources, tools);

调试

你可以在用户系统日志目录中找到服务器的日志:

Linux:   ~/.local/share/logs
macOS:   ~/Library/Logs
Windows: %USERPROFILE%\AppData\Local\Logs