DiscoverAISkills中文版
Building Your First MCP Skill: A Step-by-Step Tutorial
返回博客
TutorialDevelopmentTypeScript

Building Your First MCP Skill: A Step-by-Step Tutorial

2025年2月20日10 分钟阅读🌐 English

Build Your Own MCP Skill

Creating an MCP skill is easier than you think. In this tutorial, we'll build a weather skill that fetches real-time forecasts.

Prerequisites

  • Node.js 18+
  • TypeScript knowledge
  • An OpenWeatherMap API key (free tier works)

Step 1: Initialize the Project

mkdir weather-mcp
cd weather-mcp
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node

Step 2: Create the Server

import { Server } from "@modelcontextprotocol/sdk/server/index.js"
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"

const server = new Server(
  { name: "weather", version: "1.0.0" },
  { capabilities: { tools: {} } }
)

server.setRequestHandler("tools/list", async () => ({
  tools: [{
    name: "get_weather",
    description: "Get current weather for a city",
    inputSchema: {
      type: "object",
      properties: {
        city: { type: "string", description: "City name" }
      },
      required: ["city"]
    }
  }]
}))

server.setRequestHandler("tools/call", async (request) => {
  const { city } = request.params.arguments as { city: string }
  const res = await fetch(
    `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=YOUR_KEY&units=metric`
  )
  const data = await res.json()
  return {
    content: [{
      type: "text",
      text: `${city}: ${data.main.temp}°C, ${data.weather[0].description}`
    }]
  }
})

const transport = new StdioServerTransport()
await server.connect(transport)

Step 3: Test It

Add the skill to your Claude Desktop config and ask: "What's the weather in Tokyo?"

Step 4: Publish to AgentSkills

Submit your skill at agentskills.dev/submit and share it with the community!

探索更多 AI 技能

发现并安装最好的 AI 智能体技能,提升你的工作效率。

浏览全部技能 →