Skip to content

Latest commit

 

History

History
1133 lines (873 loc) · 47.9 KB

File metadata and controls

1133 lines (873 loc) · 47.9 KB

เริ่มต้นกับ MCP

ยินดีต้อนรับสู่ก้าวแรกของคุณกับ Model Context Protocol (MCP)! ไม่ว่าคุณจะเป็นมือใหม่กับ MCP หรืออยากเพิ่มพูนความเข้าใจ คู่มือนี้จะพาคุณผ่านขั้นตอนการตั้งค่าและพัฒนาที่จำเป็น คุณจะได้เรียนรู้ว่า MCP ช่วยให้การเชื่อมต่อระหว่างโมเดล AI กับแอปพลิเคชันเป็นไปอย่างราบรื่นอย่างไร และวิธีเตรียมสภาพแวดล้อมของคุณให้พร้อมสำหรับการสร้างและทดสอบโซลูชันที่ขับเคลื่อนด้วย MCP อย่างรวดเร็ว

TLDR; ถ้าคุณสร้างแอป AI คุณคงรู้ว่าคุณสามารถเพิ่มเครื่องมือและทรัพยากรอื่นๆ ให้กับ LLM (large language model) เพื่อทำให้ LLM มีความรู้มากขึ้น แต่ถ้าคุณวางเครื่องมือและทรัพยากรเหล่านั้นบนเซิร์ฟเวอร์ แอปและความสามารถของเซิร์ฟเวอร์นั้นก็สามารถถูกใช้งานโดยลูกค้าใดๆ ที่มีหรือไม่มี LLM ก็ได้

ภาพรวม

บทเรียนนี้ให้คำแนะนำเชิงปฏิบัติในการตั้งค่าสภาพแวดล้อม MCP และการสร้างแอป MCP แรกของคุณ คุณจะได้เรียนรู้วิธีตั้งค่าเครื่องมือและเฟรมเวิร์กที่จำเป็น สร้างเซิร์ฟเวอร์ MCP เบื้องต้น สร้างแอปโฮสต์ และทดสอบการใช้งานของคุณ

Model Context Protocol (MCP) คือโปรโตคอลเปิดที่กำหนดมาตรฐานวิธีที่แอปพลิเคชันให้บริบทกับ LLM คิดว่า MCP เหมือนพอร์ต USB-C สำหรับแอป AI — มันให้วิธีมาตรฐานในการเชื่อมต่อโมเดล AI กับแหล่งข้อมูลและเครื่องมือต่างๆ

วัตถุประสงค์การเรียนรู้

เมื่อจบบทเรียนนี้ คุณจะสามารถ:

  • ตั้งค่าสภาพแวดล้อมการพัฒนาสำหรับ MCP ใน C#, Java, Python, TypeScript และ JavaScript
  • สร้างและปรับใช้เซิร์ฟเวอร์ MCP เบื้องต้นพร้อมฟีเจอร์ที่กำหนดเอง (ทรัพยากร, prompts, และเครื่องมือ)
  • สร้างแอปโฮสต์ที่เชื่อมต่อกับเซิร์ฟเวอร์ MCP
  • ทดสอบและดีบักการใช้งาน MCP

การตั้งค่าสภาพแวดล้อม MCP ของคุณ

ก่อนเริ่มทำงานกับ MCP สิ่งสำคัญคือการเตรียมสภาพแวดล้อมการพัฒนาและเข้าใจกระบวนการทำงานพื้นฐาน ส่วนนี้จะพาคุณผ่านขั้นตอนการตั้งค่าเริ่มต้นเพื่อให้เริ่มต้นกับ MCP ได้อย่างราบรื่น

สิ่งที่ต้องเตรียม

ก่อนเริ่มพัฒนา MCP ให้แน่ใจว่าคุณมี:

  • สภาพแวดล้อมการพัฒนา สำหรับภาษาที่คุณเลือก (C#, Java, Python, TypeScript หรือ JavaScript)
  • IDE/Editor เช่น Visual Studio, Visual Studio Code, IntelliJ, Eclipse, PyCharm หรือโปรแกรมแก้ไขโค้ดสมัยใหม่อื่นๆ
  • ตัวจัดการแพ็กเกจ เช่น NuGet, Maven/Gradle, pip หรือ npm/yarn
  • API Keys สำหรับบริการ AI ที่คุณวางแผนจะใช้ในแอปโฮสต์ของคุณ

โครงสร้างเซิร์ฟเวอร์ MCP เบื้องต้น

เซิร์ฟเวอร์ MCP โดยทั่วไปประกอบด้วย:

  • การตั้งค่าเซิร์ฟเวอร์: กำหนดพอร์ต, การตรวจสอบสิทธิ์ และการตั้งค่าอื่นๆ
  • ทรัพยากร: ข้อมูลและบริบทที่เปิดให้ LLM ใช้งาน
  • เครื่องมือ: ฟังก์ชันที่โมเดลสามารถเรียกใช้
  • Prompts: เทมเพลตสำหรับสร้างหรือจัดโครงสร้างข้อความ

ตัวอย่างง่ายๆ ใน TypeScript:

import { Server, Tool, Resource } from "@modelcontextprotocol/typescript-server-sdk";

// Create a new MCP server
const server = new Server({
  port: 3000,
  name: "Example MCP Server",
  version: "1.0.0"
});

// Register a tool
server.registerTool({
  name: "calculator",
  description: "Performs basic calculations",
  parameters: {
    expression: {
      type: "string",
      description: "The math expression to evaluate"
    }
  },
  handler: async (params) => {
    const result = eval(params.expression);
    return { result };
  }
});

// Start the server
server.start();

ในโค้ดข้างต้น เราได้:

  • นำเข้าคลาสที่จำเป็นจาก MCP TypeScript SDK
  • สร้างและตั้งค่าอินสแตนซ์เซิร์ฟเวอร์ MCP ใหม่
  • ลงทะเบียนเครื่องมือที่กำหนดเอง (calculator) พร้อมฟังก์ชันจัดการ
  • เริ่มเซิร์ฟเวอร์เพื่อรอฟังคำขอ MCP ที่เข้ามา

การทดสอบและดีบัก

ก่อนเริ่มทดสอบเซิร์ฟเวอร์ MCP ของคุณ สิ่งสำคัญคือการเข้าใจเครื่องมือที่มีและแนวทางปฏิบัติที่ดีที่สุดสำหรับการดีบัก การทดสอบอย่างมีประสิทธิภาพช่วยให้เซิร์ฟเวอร์ทำงานตามที่คาดหวังและช่วยให้คุณระบุและแก้ไขปัญหาได้อย่างรวดเร็ว ส่วนถัดไปนี้จะอธิบายวิธีการที่แนะนำสำหรับการตรวจสอบการใช้งาน MCP ของคุณ

MCP มีเครื่องมือช่วยทดสอบและดีบักเซิร์ฟเวอร์ของคุณ:

  • Inspector tool อินเทอร์เฟซกราฟิกที่ช่วยให้คุณเชื่อมต่อกับเซิร์ฟเวอร์และทดสอบเครื่องมือ, prompts และทรัพยากรของคุณ
  • curl คุณยังสามารถเชื่อมต่อกับเซิร์ฟเวอร์โดยใช้เครื่องมือบรรทัดคำสั่งอย่าง curl หรือไคลเอนต์อื่นๆ ที่สามารถสร้างและรันคำสั่ง HTTP ได้

การใช้ MCP Inspector

MCP Inspector คือเครื่องมือทดสอบแบบภาพที่ช่วยให้คุณ:

  1. ค้นพบความสามารถของเซิร์ฟเวอร์: ตรวจจับทรัพยากร, เครื่องมือ และ prompts ที่มีโดยอัตโนมัติ
  2. ทดสอบการทำงานของเครื่องมือ: ลองพารามิเตอร์ต่างๆ และดูผลลัพธ์แบบเรียลไทม์
  3. ดูข้อมูลเมตาของเซิร์ฟเวอร์: ตรวจสอบข้อมูลเซิร์ฟเวอร์, สคีมา และการตั้งค่า
# ex TypeScript, installing and running MCP Inspector
npx @modelcontextprotocol/inspector node build/index.js

เมื่อคุณรันคำสั่งข้างต้น MCP Inspector จะเปิดอินเทอร์เฟซเว็บในเครื่องของคุณผ่านเบราว์เซอร์ คุณจะเห็นแดชบอร์ดที่แสดงเซิร์ฟเวอร์ MCP ที่ลงทะเบียนไว้ เครื่องมือ ทรัพยากร และ prompts ที่มี อินเทอร์เฟซนี้ช่วยให้คุณทดสอบการทำงานของเครื่องมือแบบโต้ตอบ ตรวจสอบข้อมูลเมตาของเซิร์ฟเวอร์ และดูผลลัพธ์แบบเรียลไทม์ ทำให้การตรวจสอบและดีบักเซิร์ฟเวอร์ MCP ของคุณง่ายขึ้น

นี่คือตัวอย่างภาพหน้าจอ:

ปัญหาการตั้งค่าที่พบบ่อยและวิธีแก้ไข

ปัญหา วิธีแก้ไขที่เป็นไปได้
การเชื่อมต่อล้มเหลว ตรวจสอบว่าเซิร์ฟเวอร์กำลังทำงานและพอร์ตถูกต้อง
ข้อผิดพลาดในการทำงานของเครื่องมือ ตรวจสอบการตรวจสอบพารามิเตอร์และการจัดการข้อผิดพลาด
การตรวจสอบสิทธิ์ล้มเหลว ตรวจสอบ API keys และสิทธิ์การใช้งาน
ข้อผิดพลาดการตรวจสอบสคีมา ตรวจสอบให้แน่ใจว่าพารามิเตอร์ตรงกับสคีมาที่กำหนด
เซิร์ฟเวอร์ไม่เริ่มทำงาน ตรวจสอบความขัดแย้งของพอร์ตหรือการขาดไลบรารีที่จำเป็น
ข้อผิดพลาด CORS ตั้งค่า header CORS ให้ถูกต้องสำหรับคำขอข้ามแหล่งที่มา
ปัญหาการตรวจสอบสิทธิ์ ตรวจสอบความถูกต้องของโทเค็นและสิทธิ์การใช้งาน

การพัฒนาในเครื่อง

สำหรับการพัฒนาและทดสอบในเครื่อง คุณสามารถรันเซิร์ฟเวอร์ MCP บนเครื่องของคุณได้โดยตรง:

  1. เริ่มกระบวนการเซิร์ฟเวอร์: รันแอปเซิร์ฟเวอร์ MCP ของคุณ
  2. ตั้งค่าเครือข่าย: ตรวจสอบให้แน่ใจว่าเซิร์ฟเวอร์เข้าถึงได้ผ่านพอร์ตที่คาดหวัง
  3. เชื่อมต่อไคลเอนต์: ใช้ URL การเชื่อมต่อในเครื่อง เช่น http://localhost:3000
# Example: Running a TypeScript MCP server locally
npm run start
# Server running at http://localhost:3000

การสร้างเซิร์ฟเวอร์ MCP แรกของคุณ

เราได้พูดถึง แนวคิดหลัก ในบทเรียนก่อนหน้าแล้ว ตอนนี้ถึงเวลานำความรู้นั้นมาใช้จริง

เซิร์ฟเวอร์ทำอะไรได้บ้าง

ก่อนเริ่มเขียนโค้ด มาทบทวนกันว่าเซิร์ฟเวอร์ทำอะไรได้บ้าง:

เซิร์ฟเวอร์ MCP สามารถ:

  • เข้าถึงไฟล์และฐานข้อมูลในเครื่อง
  • เชื่อมต่อกับ API ระยะไกล
  • ทำการคำนวณ
  • รวมเข้ากับเครื่องมือและบริการอื่นๆ
  • ให้ส่วนติดต่อผู้ใช้สำหรับการโต้ตอบ

ดีมาก ตอนนี้ที่เรารู้ว่าเซิร์ฟเวอร์ทำอะไรได้บ้าง มาเริ่มเขียนโค้ดกันเลย

แบบฝึกหัด: การสร้างเซิร์ฟเวอร์

ในการสร้างเซิร์ฟเวอร์ คุณต้องทำตามขั้นตอนเหล่านี้:

  • ติดตั้ง MCP SDK
  • สร้างโปรเจกต์และตั้งโครงสร้างโปรเจกต์
  • เขียนโค้ดเซิร์ฟเวอร์
  • ทดสอบเซิร์ฟเวอร์

-1- ติดตั้ง SDK

ขั้นตอนนี้จะแตกต่างกันเล็กน้อยตาม runtime ที่คุณเลือก เลือก runtime ที่เหมาะสมจากด้านล่าง:

Note

สำหรับ Python เราจะสร้างโครงสร้างโปรเจกต์ก่อน แล้วจึงติดตั้ง dependencies

TypeScript

npm install @modelcontextprotocol/sdk zod
npm install -D @types/node typescript

Python

# Create project dir
mkdir calculator-server
cd calculator-server
# Open the folder in Visual Studio Code - Skip this if you are using a different IDE
code .

.NET

dotnet new console -n McpCalculatorServer
cd McpCalculatorServer

Java

สำหรับ Java ให้สร้างโปรเจกต์ Spring Boot:

curl https://start.spring.io/starter.zip \
  -d dependencies=web \
  -d javaVersion=21 \
  -d type=maven-project \
  -d groupId=com.example \
  -d artifactId=calculator-server \
  -d name=McpServer \
  -d packageName=com.microsoft.mcp.sample.server \
  -o calculator-server.zip

แตกไฟล์ zip:

unzip calculator-server.zip -d calculator-server
cd calculator-server
# optional remove the unused test
rm -rf src/test/java

เพิ่มการตั้งค่าครบถ้วนนี้ในไฟล์ pom.xml ของคุณ:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <!-- Spring Boot parent for dependency management -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.0</version>
        <relativePath />
    </parent>

    <!-- Project coordinates -->
    <groupId>com.example</groupId>
    <artifactId>calculator-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Calculator Server</name>
    <description>Basic calculator MCP service for beginners</description>

    <!-- Properties -->
    <properties>
        <java.version>21</java.version>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>

    <!-- Spring AI BOM for version management -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- Dependencies -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
    </dependencies>

    <!-- Build configuration -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <release>21</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- Repositories for Spring AI snapshots -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
</project>

-2- สร้างโปรเจกต์

เมื่อคุณติดตั้ง SDK แล้ว มาสร้างโปรเจกต์กันต่อ:

TypeScript

mkdir src
npm install -y

Python

# Create a virtual env and install dependencies
python -m venv venv
venv\Scripts\activate
pip install "mcp[cli]"

Java

cd calculator-server
./mvnw clean install -DskipTests

-3- สร้างไฟล์โปรเจกต์

TypeScript

สร้างไฟล์ package.json ด้วยเนื้อหาดังนี้:

{
   "type": "module",
   "bin": {
     "weather": "./build/index.js"
   },
   "scripts": {
     "build": "tsc && node build/index.js"
   },
   "files": [
     "build"
   ]
}

สร้างไฟล์ tsconfig.json ด้วยเนื้อหาดังนี้:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Python

สร้างไฟล์ server.py

touch server.py

.NET

ติดตั้งแพ็กเกจ NuGet ที่จำเป็น:

dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting

Java

สำหรับโปรเจกต์ Java Spring Boot โครงสร้างโปรเจกต์จะถูกสร้างโดยอัตโนมัติ

-4- เขียนโค้ดเซิร์ฟเวอร์

TypeScript

สร้างไฟล์ index.ts และเพิ่มโค้ดดังนี้:

import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
 
// Create an MCP server
const server = new McpServer({
  name: "Demo",
  version: "1.0.0"
});

ตอนนี้คุณมีเซิร์ฟเวอร์แล้ว แต่ยังทำงานได้น้อยอยู่ มาแก้ไขกัน

Python

# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo")

.NET

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();
await builder.Build().RunAsync();

// add features

Java

สำหรับ Java สร้างคอมโพเนนต์หลักของเซิร์ฟเวอร์ก่อน โดยแก้ไขคลาสแอปพลิเคชันหลัก:

src/main/java/com/microsoft/mcp/sample/server/McpServerApplication.java:

package com.microsoft.mcp.sample.server;

import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.microsoft.mcp.sample.server.service.CalculatorService;

@SpringBootApplication
public class McpServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(McpServerApplication.class, args);
    }
    
    @Bean
    public ToolCallbackProvider calculatorTools(CalculatorService calculator) {
        return MethodToolCallbackProvider.builder().toolObjects(calculator).build();
    }
}

สร้างบริการเครื่องคิดเลข src/main/java/com/microsoft/mcp/sample/server/service/CalculatorService.java:

package com.microsoft.mcp.sample.server.service;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

/**
 * Service for basic calculator operations.
 * This service provides simple calculator functionality through MCP.
 */
@Service
public class CalculatorService {

    /**
     * Add two numbers
     * @param a The first number
     * @param b The second number
     * @return The sum of the two numbers
     */
    @Tool(description = "Add two numbers together")
    public String add(double a, double b) {
        double result = a + b;
        return formatResult(a, "+", b, result);
    }

    /**
     * Subtract one number from another
     * @param a The number to subtract from
     * @param b The number to subtract
     * @return The result of the subtraction
     */
    @Tool(description = "Subtract the second number from the first number")
    public String subtract(double a, double b) {
        double result = a - b;
        return formatResult(a, "-", b, result);
    }

    /**
     * Multiply two numbers
     * @param a The first number
     * @param b The second number
     * @return The product of the two numbers
     */
    @Tool(description = "Multiply two numbers together")
    public String multiply(double a, double b) {
        double result = a * b;
        return formatResult(a, "*", b, result);
    }

    /**
     * Divide one number by another
     * @param a The numerator
     * @param b The denominator
     * @return The result of the division
     */
    @Tool(description = "Divide the first number by the second number")
    public String divide(double a, double b) {
        if (b == 0) {
            return "Error: Cannot divide by zero";
        }
        double result = a / b;
        return formatResult(a, "/", b, result);
    }

    /**
     * Calculate the power of a number
     * @param base The base number
     * @param exponent The exponent
     * @return The result of raising the base to the exponent
     */
    @Tool(description = "Calculate the power of a number (base raised to an exponent)")
    public String power(double base, double exponent) {
        double result = Math.pow(base, exponent);
        return formatResult(base, "^", exponent, result);
    }

    /**
     * Calculate the square root of a number
     * @param number The number to find the square root of
     * @return The square root of the number
     */
    @Tool(description = "Calculate the square root of a number")
    public String squareRoot(double number) {
        if (number < 0) {
            return "Error: Cannot calculate square root of a negative number";
        }
        double result = Math.sqrt(number);
        return String.format("√%.2f = %.2f", number, result);
    }

    /**
     * Calculate the modulus (remainder) of division
     * @param a The dividend
     * @param b The divisor
     * @return The remainder of the division
     */
    @Tool(description = "Calculate the remainder when one number is divided by another")
    public String modulus(double a, double b) {
        if (b == 0) {
            return "Error: Cannot divide by zero";
        }
        double result = a % b;
        return formatResult(a, "%", b, result);
    }

    /**
     * Calculate the absolute value of a number
     * @param number The number to find the absolute value of
     * @return The absolute value of the number
     */
    @Tool(description = "Calculate the absolute value of a number")
    public String absolute(double number) {
        double result = Math.abs(number);
        return String.format("|%.2f| = %.2f", number, result);
    }

    /**
     * Get help about available calculator operations
     * @return Information about available operations
     */
    @Tool(description = "Get help about available calculator operations")
    public String help() {
        return "Basic Calculator MCP Service\n\n" +
               "Available operations:\n" +
               "1. add(a, b) - Adds two numbers\n" +
               "2. subtract(a, b) - Subtracts the second number from the first\n" +
               "3. multiply(a, b) - Multiplies two numbers\n" +
               "4. divide(a, b) - Divides the first number by the second\n" +
               "5. power(base, exponent) - Raises a number to a power\n" +
               "6. squareRoot(number) - Calculates the square root\n" + 
               "7. modulus(a, b) - Calculates the remainder of division\n" +
               "8. absolute(number) - Calculates the absolute value\n\n" +
               "Example usage: add(5, 3) will return 5 + 3 = 8";
    }

    /**
     * Format the result of a calculation
     */
    private String formatResult(double a, String operator, double b, double result) {
        return String.format("%.2f %s %.2f = %.2f", a, operator, b, result);
    }
}

คอมโพเนนต์เสริมสำหรับบริการที่พร้อมใช้งานจริง:

สร้างการตั้งค่าเริ่มต้น src/main/java/com/microsoft/mcp/sample/server/config/StartupConfig.java:

package com.microsoft.mcp.sample.server.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StartupConfig {
    
    @Bean
    public CommandLineRunner startupInfo() {
        return args -> {
            System.out.println("\n" + "=".repeat(60));
            System.out.println("Calculator MCP Server is starting...");
            System.out.println("SSE endpoint: http://localhost:8080/sse");
            System.out.println("Health check: http://localhost:8080/actuator/health");
            System.out.println("=".repeat(60) + "\n");
        };
    }
}

สร้างคอนโทรลเลอร์ตรวจสุขภาพ src/main/java/com/microsoft/mcp/sample/server/controller/HealthController.java:

package com.microsoft.mcp.sample.server.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HealthController {
    
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("service", "Calculator MCP Server");
        return ResponseEntity.ok(response);
    }
}

สร้างตัวจัดการข้อยกเว้น src/main/java/com/microsoft/mcp/sample/server/exception/GlobalExceptionHandler.java:

package com.microsoft.mcp.sample.server.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex) {
        ErrorResponse error = new ErrorResponse(
            "Invalid_Input", 
            "Invalid input parameter: " + ex.getMessage());
        return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
    }

    public static class ErrorResponse {
        private String code;
        private String message;

        public ErrorResponse(String code, String message) {
            this.code = code;
            this.message = message;
        }

        // Getters
        public String getCode() { return code; }
        public String getMessage() { return message; }
    }
}

สร้างแบนเนอร์แบบกำหนดเอง src/main/resources/banner.txt:

_____      _            _       _             
 / ____|    | |          | |     | |            
| |     __ _| | ___ _   _| | __ _| |_ ___  _ __ 
| |    / _` | |/ __| | | | |/ _` | __/ _ \| '__|
| |___| (_| | | (__| |_| | | (_| | || (_) | |   
 \_____\__,_|_|\___|\__,_|_|\__,_|\__\___/|_|   
                                                
Calculator MCP Server v1.0
Spring Boot MCP Application

-5- เพิ่มเครื่องมือและทรัพยากร

เพิ่มเครื่องมือและทรัพยากรโดยเพิ่มโค้ดดังนี้:

TypeScript

  server.tool("add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

server.resource(
  "greeting",
  new ResourceTemplate("greeting://{name}", { list: undefined }),
  async (uri, { name }) => ({
    contents: [{
      uri: uri.href,
      text: `Hello, ${name}!`
    }]
  })
);

เครื่องมือของคุณรับพารามิเตอร์ a และ b และรันฟังก์ชันที่สร้างการตอบกลับในรูปแบบ:

{
  contents: [{
    type: "text", content: "some content"
  }]
}

ทรัพยากรของคุณเข้าถึงผ่านสตริง "greeting" และรับพารามิเตอร์ name พร้อมสร้างการตอบกลับในลักษณะเดียวกับเครื่องมือ:

{
  uri: "<href>",
  text: "a text"
}

Python

# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b


# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"

ในโค้ดข้างต้น เราได้:

  • กำหนดเครื่องมือ add ที่รับพารามิเตอร์ a และ p ซึ่งเป็นจำนวนเต็มทั้งคู่
  • สร้างทรัพยากรชื่อ greeting ที่รับพารามิเตอร์ name

.NET

เพิ่มโค้ดนี้ในไฟล์ Program.cs ของคุณ:

[McpServerToolType]
public static class CalculatorTool
{
    [McpServerTool, Description("Adds two numbers")]
    public static string Add(int a, int b) => $"Sum {a + b}";
}

Java

เครื่องมือถูกสร้างไว้แล้วในขั้นตอนก่อนหน้า

-6- โค้ดสุดท้าย

เพิ่มโค้ดสุดท้ายที่จำเป็นเพื่อให้เซิร์ฟเวอร์เริ่มทำงานได้:

TypeScript

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);

นี่คือโค้ดทั้งหมด:

// index.ts
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create an MCP server
const server = new McpServer({
  name: "Demo",
  version: "1.0.0"
});

// Add an addition tool
server.tool("add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

// Add a dynamic greeting resource
server.resource(
  "greeting",
  new ResourceTemplate("greeting://{name}", { list: undefined }),
  async (uri, { name }) => ({
    contents: [{
      uri: uri.href,
      text: `Hello, ${name}!`
    }]
  })
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);

Python

# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo")


# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b


# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"

# Main execution block - this is required to run the server
if __name__ == "__main__":
    mcp.run()

.NET

สร้างไฟล์ Program.cs ด้วยเนื้อหาดังนี้:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();
await builder.Build().RunAsync();

[McpServerToolType]
public static class CalculatorTool
{
    [McpServerTool, Description("Adds two numbers")]
    public static string Add(int a, int b) => $"Sum {a + b}";
}

Java

คลาสแอปพลิเคชันหลักของคุณควรมีลักษณะดังนี้:

// McpServerApplication.java
package com.microsoft.mcp.sample.server;

import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.microsoft.mcp.sample.server.service.CalculatorService;

@SpringBootApplication
public class McpServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(McpServerApplication.class, args);
    }
    
    @Bean
    public ToolCallbackProvider calculatorTools(CalculatorService calculator) {
        return MethodToolCallbackProvider.builder().toolObjects(calculator).build();
    }
}

-7- ทดสอบเซิร์ฟเวอร์

เริ่มเซิร์ฟเวอร์ด้วยคำสั่งนี้:

TypeScript

npm run build

Python

mcp run server.py

ในการใช้ MCP Inspector ให้ใช้คำสั่ง mcp dev server.py ซึ่งจะเปิด Inspector อัตโนมัติและให้โทเค็นเซสชันพร็อกซีที่จำเป็น หากใช้ mcp run server.py คุณจะต้องเปิด Inspector ด้วยตนเองและตั้งค่าการเชื่อมต่อ

.NET

ตรวจสอบว่าคุณอยู่ในไดเรกทอรีโปรเจกต์:

cd McpCalculatorServer
dotnet run

Java

./mvnw clean install -DskipTests
java -jar target/calculator-server-0.0.1-SNAPSHOT.jar

-8- รันด้วย Inspector

Inspector เป็นเครื่องมือที่ยอดเยี่ยมที่ช่วยเริ่มเซิร์ฟเวอร์และให้คุณโต้ตอบกับมันเพื่อทดสอบการทำงาน มาเริ่มกันเลย:

Note

อาจดูแตกต่างในช่อง "command" เพราะมีคำสั่งสำหรับรันเซิร์ฟเวอร์ด้วย runtime เฉพาะของคุณ

TypeScript

npx @modelcontextprotocol/inspector node build/index.js

หรือเพิ่มใน package.json ของคุณแบบนี้: "inspector": "npx @modelcontextprotocol/inspector node build/index.js" แล้วรัน npm run inspect

Python ใช้เครื่องมือ Node.js ที่ชื่อ inspector ห่อหุ้มไว้ คุณสามารถเรียกใช้เครื่องมือนี้ได้แบบนี้:

mcp dev server.py

อย่างไรก็ตาม มันไม่ได้รองรับทุกเมธอดที่มีในเครื่องมือ จึงแนะนำให้รันเครื่องมือ Node.js โดยตรงแบบนี้:

npx @modelcontextprotocol/inspector mcp run server.py

ถ้าคุณใช้เครื่องมือหรือ IDE ที่อนุญาตให้ตั้งค่าคำสั่งและอาร์กิวเมนต์สำหรับรันสคริปต์
ให้ตั้ง python ในช่อง Command และ server.py ในช่อง Arguments เพื่อให้สคริปต์ทำงานถูกต้อง

.NET

ตรวจสอบว่าคุณอยู่ในไดเรกทอรีโปรเจกต์:

cd McpCalculatorServer
npx @modelcontextprotocol/inspector dotnet run

Java

ตรวจสอบให้แน่ใจว่าเซิร์ฟเวอร์ calculator ของคุณกำลังทำงาน
จากนั้นรัน inspector:

npx @modelcontextprotocol/inspector

ในอินเทอร์เฟซเว็บของ inspector:

  1. เลือก "SSE" เป็นประเภทการส่งข้อมูล
  2. ตั้งค่า URL เป็น: http://localhost:8080/sse
  3. คลิก "Connect"

Connect

คุณเชื่อมต่อกับเซิร์ฟเวอร์เรียบร้อยแล้ว
ส่วนการทดสอบเซิร์ฟเวอร์ Java เสร็จสมบูรณ์แล้ว

ส่วนถัดไปจะเป็นเรื่องการโต้ตอบกับเซิร์ฟเวอร์

คุณควรเห็นส่วนติดต่อผู้ใช้ดังนี้:

Connect

  1. เชื่อมต่อกับเซิร์ฟเวอร์โดยกดปุ่ม Connect
    เมื่อเชื่อมต่อแล้ว คุณจะเห็นดังนี้:

    Connected

  2. เลือก "Tools" และ "listTools" คุณจะเห็น "Add" ปรากฏขึ้น เลือก "Add" และกรอกค่าพารามิเตอร์

    คุณจะเห็นผลลัพธ์ดังนี้ คือผลลัพธ์จากเครื่องมือ "add":

    Result of running add

ยินดีด้วย คุณสร้างและรันเซิร์ฟเวอร์แรกของคุณสำเร็จแล้ว!

SDK อย่างเป็นทางการ

MCP มี SDK อย่างเป็น

  • Kotlin SDK - การใช้งาน Kotlin อย่างเป็นทางการ
  • Swift SDK - ดูแลร่วมกับ Loopwork AI
  • Rust SDK - การใช้งาน Rust อย่างเป็นทางการ

ประเด็นสำคัญ

  • การตั้งค่าสภาพแวดล้อมการพัฒนา MCP ทำได้ง่ายด้วย SDK เฉพาะภาษา
  • การสร้างเซิร์ฟเวอร์ MCP ต้องสร้างและลงทะเบียนเครื่องมือพร้อมสคีมาที่ชัดเจน
  • การทดสอบและดีบักเป็นสิ่งจำเป็นสำหรับการใช้งาน MCP ที่เชื่อถือได้

ตัวอย่าง

งานที่ได้รับมอบหมาย

สร้างเซิร์ฟเวอร์ MCP ง่ายๆ พร้อมเครื่องมือที่คุณเลือก:

  1. เขียนเครื่องมือด้วยภาษาที่คุณถนัด (.NET, Java, Python หรือ JavaScript)
  2. กำหนดพารามิเตอร์นำเข้าและค่าที่ส่งกลับ
  3. รันเครื่องมือตรวจสอบเพื่อให้แน่ใจว่าเซิร์ฟเวอร์ทำงานตามที่ตั้งใจไว้
  4. ทดสอบการใช้งานด้วยข้อมูลนำเข้าหลากหลายรูปแบบ

วิธีแก้ไข

Solution

แหล่งข้อมูลเพิ่มเติม

ต่อไป

ถัดไป: เริ่มต้นกับ MCP Clients

ข้อจำกัดความรับผิดชอบ:
เอกสารนี้ได้รับการแปลโดยใช้บริการแปลภาษาอัตโนมัติ Co-op Translator แม้เราจะพยายามให้ความถูกต้องสูงสุด แต่โปรดทราบว่าการแปลอัตโนมัติอาจมีข้อผิดพลาดหรือความไม่ถูกต้อง เอกสารต้นฉบับในภาษาต้นทางถือเป็นแหล่งข้อมูลที่เชื่อถือได้ สำหรับข้อมูลที่สำคัญ ขอแนะนำให้ใช้บริการแปลโดยผู้เชี่ยวชาญมนุษย์ เราไม่รับผิดชอบต่อความเข้าใจผิดหรือการตีความผิดใด ๆ ที่เกิดจากการใช้การแปลนี้