← 返回 Skills 市场
sky-lv

Skylv Mcp Server Creator

作者 SKY-lv · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ⚠ suspicious
49
总下载
0
收藏
1
当前安装
1
版本数
在 OpenClaw 中安装
/install skylv-mcp-server-creator
功能描述
Build and run Model Context Protocol (MCP) servers with customizable tools and resources using TypeScript and the MCP SDK.
使用说明 (SKILL.md)

MCP Server Builder

功能说明

构建 Model Context Protocol 服务器,扩展 AI 能力边界。

MCP 协议概述

MCP 是 Anthropic 推出的 AI 模型上下文协议,让 AI 能调用外部工具和数据源。

项目结构

mcp-server/
├── package.json
├── tsconfig.json
├── src/
│   ├── index.ts          # 主入口
│   ├── tools/            # 工具定义
│   └── resources/         # 资源定义
└── tsconfig.json

完整实现

1. 初始化项目

npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node ts-node
// package.json
{
  "name": "my-mcp-server",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "ts-node src/index.ts"
  },
  "dependencies": {
    "@modelcontextprotocol/sdk": "^0.5.0",
    "zod": "^3.22.0"
  }
}
// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"]
}

2. 定义工具

// src/tools/search.ts
import { z } from 'zod';

export const searchTool = {
  name: 'web_search',
  description: '搜索互联网获取最新信息',
  inputSchema: z.object({
    query: z.string().describe('搜索关键词'),
    limit: z.number().optional().default(5).describe('返回结果数量')
  }),

  async handler(args: { query: string; limit?: number }) {
    // 实际实现
    const results = await performSearch(args.query, args.limit || 5);
    return {
      content: results.map(r => ({
        type: 'text' as const,
        text: `标题: ${r.title}\
链接: ${r.url}\
摘要: ${r.snippet}`
      }))
    };
  }
};

3. 定义资源

// src/resources/knowledge.ts
export const knowledgeResources = {
  uriPrefix: 'knowledge://',

  list: async () => [
    {
      uri: 'knowledge://docs/latest',
      name: '最新文档',
      description: '系统最新文档版本',
      mimeType: 'text/markdown'
    }
  ],

  read: async (uri: string) => {
    if (uri === 'knowledge://docs/latest') {
      return {
        contents: [{
          uri,
          mimeType: 'text/markdown',
          text: '# 最新文档\
\
...'
        }]
      };
    }
    throw new Error('Resource not found');
  }
};

4. 主入口

// src/index.ts
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
  CallToolRequestSchema,
  ListToolsRequestSchema,
  ListResourcesRequestSchema,
  ReadResourceRequestSchema
} from '@modelcontextprotocol/sdk/types.js';

import { searchTool } from './tools/search.js';
import { knowledgeResources } from './resources/knowledge.js';

class MyMCPServer {
  private server: Server;

  constructor() {
    this.server = new Server(
      { name: 'my-mcp-server', version: '1.0.0' },
      { capabilities: { tools: {}, resources: {} } }
    );

    this.setupToolHandlers();
    this.setupResourceHandlers();
  }

  private setupToolHandlers() {
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
      tools: [
        {
          name: searchTool.name,
          description: searchTool.description,
          inputSchema: searchTool.inputSchema
        }
      ]
    }));

    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      const { name, arguments: args } = request.params;
      
      if (name === 'web_search') {
        return await searchTool.handler(args as any);
      }
      
      throw new Error(`Unknown tool: ${name}`);
    });
  }

  private setupResourceHandlers() {
    this.server.setRequestHandler(ListResourcesRequestSchema, async () => ({
      resources: await knowledgeResources.list()
    }));

    this.server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
      return await knowledgeResources.read(request.params.uri);
    });
  }

  async start() {
    const transport = new StdioServerTransport();
    await this.server.connect(transport);
    console.error('MCP Server started on stdio');
  }
}

new MyMCPServer().start().catch(console.error);

5. 更多工具示例

// 文件操作工具
export const fileTools = {
  name: 'file_operations',
  description: '读取、写入、列出文件',
  inputSchema: z.object({
    operation: z.enum(['read', 'write', 'list', 'delete']),
    path: z.string(),
    content: z.string().optional()
  }),

  async handler(args: any) {
    const fs = await import('fs/promises');
    
    switch (args.operation) {
      case 'read': {
        const content = await fs.readFile(args.path, 'utf-8');
        return { content: [{ type: 'text', text: content }] };
      }
      case 'write': {
        await fs.writeFile(args.path, args.content || '');
        return { content: [{ type: 'text', text: 'File written successfully' }] };
      }
      case 'list': {
        const files = await fs.readdir(args.path);
        return { content: [{ type: 'text', text: files.join('\
') }] };
      }
      default:
        throw new Error(`Unknown operation: ${args.operation}`);
    }
  }
};

// 数据库查询工具
export const dbTool = {
  name: 'database_query',
  description: '执行数据库查询',
  inputSchema: z.object({
    sql: z.string().describe('SQL查询语句'),
    params: z.array(z.any()).optional()
  }),

  async handler(args: any) {
    // 使用 mysql2 或 pg
    // const pool = new Pool({ connectionString: process.env.DATABASE_URL });
    // const result = await pool.query(args.sql, args.params);
    return {
      content: [{ type: 'text', text: JSON.stringify({ rows: [], count: 0 }) }]
    };
  }
};

测试

# 编译
npm run build

# 手动测试
echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | npm run dev

# MCP Inspector
npx @modelcontextprotocol/inspector npm run dev

部署

Claude Desktop

// ~/.config/claude-desktop/claude_desktop_config.json
{
  "mcpServers": {
    "my-mcp-server": {
      "command": "node",
      "args": ["/path/to/mcp-server/dist/index.js"],
      "env": {
        "API_KEY": "your-api-key"
      }
    }
  }
}

Cursor / VS Code

在扩展设置中添加 MCP 服务器路径。

最佳实践

  1. 错误处理:始终返回有意义的错误信息
  2. 类型安全:使用 Zod 严格验证输入
  3. 日志记录:使用 console.error 记录关键事件
  4. 性能:长时间操作使用流式响应
  5. 安全:不记录敏感信息,定期清理日志

Usage

  1. Install the skill
  2. Configure as needed
  3. Run with OpenClaw
安全使用建议
Review the generated MCP tools before running them. The basic MCP scaffolding is expected, but do not copy the unrestricted file or database examples into a live server without limiting paths, database privileges, allowed operations, and requiring confirmation for writes or destructive actions.
功能分析
Type: OpenClaw Skill Name: skylv-mcp-server-creator Version: 1.0.0 The skill provides boilerplate code for building MCP servers that includes high-risk capabilities such as arbitrary file system operations (read, write, delete) and raw SQL execution in the 'fileTools' and 'dbTool' examples within SKILL.md. While these features align with the stated purpose of a server builder, the provided code lacks input sanitization or path validation, creating significant vulnerabilities like Path Traversal and SQL Injection if an AI agent implements them as written.
能力标签
requires-sensitive-credentials
能力评估
Purpose & Capability
The stated MCP server-building purpose matches the instructions, but the provided tool templates include broad file and database capabilities without safety boundaries.
Instruction Scope
Most commands are user-directed setup steps, but the example MCP tools accept raw file paths and SQL strings from tool callers without allowlists, confirmations, or read-only limits.
Install Mechanism
There is no install spec or bundled executable code. The skill tells users to run npm install commands for expected MCP/TypeScript packages, but dependency versions are not tightly pinned.
Credentials
The file tool is not scoped to a project directory, and the database example could inherit whatever authority is granted through DATABASE_URL.
Persistence & Privilege
The sample starts an MCP server over stdio only when the user runs it; no autostart or hidden persistence is shown. Optional database credentials should still be least-privileged.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install skylv-mcp-server-creator
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /skylv-mcp-server-creator 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
- Initial release as "mcp-server-builder" (version 1.0.2), providing a complete scaffolding template for building Model Context Protocol (MCP) servers. - Features detailed instructions and code samples for defining tools, resources, and handlers in a TypeScript MCP server project. - Includes best practices for error handling, type safety, logging, and deployment in environments like Claude Desktop and VS Code. - Offers example implementations for web search, file operations, and database query tools. - Supports local testing and integration with OpenClaw and MCP Inspector.
元数据
Slug skylv-mcp-server-creator
版本 1.0.0
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 1
常见问题

Skylv Mcp Server Creator 是什么?

Build and run Model Context Protocol (MCP) servers with customizable tools and resources using TypeScript and the MCP SDK. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 49 次。

如何安装 Skylv Mcp Server Creator?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install skylv-mcp-server-creator」即可一键安装,无需额外配置。

Skylv Mcp Server Creator 是免费的吗?

是的,Skylv Mcp Server Creator 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Skylv Mcp Server Creator 支持哪些平台?

Skylv Mcp Server Creator 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Skylv Mcp Server Creator?

由 SKY-lv(@sky-lv)开发并维护,当前版本 v1.0.0。

💬 留言讨论