Sponsored by Deepsite.site

Anthropic API Server (TypeScript)

Created By
ptitiwat25438 months ago
Content

Anthropic API Server (TypeScript)

บริการ API สำหรับ Anthropic Claude ที่มีความครอบคลุมการทดสอบสูง ช่วยให้เชื่อมต่อกับ Claude (Anthropic API) ได้อย่างปลอดภัย

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

mcp-p-anthropic-api-server/
├── bin/                      # สคริปต์สำหรับรันผ่าน CLI
├── src/                      # โค้ดหลักของโปรเจกต์
│   ├── middleware/           # Express middleware
│   ├── routes/               # Express routes
│   ├── services/             # บริการที่ใช้ภายในแอป
│   ├── __tests__/            # ไฟล์ทดสอบ
│   │   └── __mocks__/        # ไฟล์จำลองสำหรับการทดสอบ
│   ├── config.ts             # การตั้งค่าแอปพลิเคชัน
│   ├── index.ts              # จุดเริ่มต้นของแอปพลิเคชัน
│   ├── logger.ts             # ระบบบันทึกข้อมูล
│   └── server.ts             # ตัวสร้างและจัดการเซิร์ฟเวอร์
├── examples/                 # ตัวอย่างการใช้งานโดยตรง
├── tests/                    # ไฟล์ทดสอบเพิ่มเติม
├── types/                    # ไฟล์นิยามประเภทข้อมูล
├── docs/                     # เอกสารทางเทคนิคสำหรับนักพัฒนา
├── public-docs/              # เอกสารสำหรับผู้ใช้ทั่วไป
├── .env                      # ตัวแปรสภาพแวดล้อม (ไม่ควรอัปโหลดไป git)
├── .env.example              # ตัวอย่างไฟล์ .env
├── .gitignore                # ไฟล์ที่ระบุให้ git ไม่สนใจ
├── CHANGELOG.md              # บันทึกการเปลี่ยนแปลง
├── CONTRIBUTING.md           # คำแนะนำสำหรับผู้ร่วมพัฒนา
├── LICENSE                   # สัญญาอนุญาต
├── package.json              # ข้อมูลแพ็กเกจและการพึ่งพา npm
├── README.md                 # เอกสารนี้
└── tsconfig.json             # การตั้งค่า TypeScript

คุณสมบัติหลัก

  • เชื่อมต่อกับ Anthropic API: ใช้ SDK อย่างเป็นทางการเพื่อความปลอดภัยและประสิทธิภาพ
  • รองรับ Streaming Response: สามารถใช้งาน streaming สำหรับการตอบกลับแบบ real-time
  • รองรับ Function Calling / Tools: ให้ Claude เรียกใช้เครื่องมือต่างๆ ได้
  • การตรวจสอบข้อมูลนำเข้า: ใช้ Zod Schema เพื่อตรวจสอบความถูกต้องของคำขอ
  • ระบบบันทึกข้อมูล: บันทึกการทำงานของระบบอย่างละเอียด
  • เรียกใช้ผ่าน npx: ติดตั้งและใช้งานได้ง่ายผ่าน npx
  • ระบบจัดการ API Key: อ่านค่า API Key จากไฟล์ .env

การติดตั้ง

  1. โคลนโปรเจกต์
git clone https://github.com/ptitiwat2543/mcp-p-anthropic-api.git
cd mcp-p-anthropic-api
  1. ติดตั้งการพึ่งพา
npm install
  1. สร้างไฟล์ .env (คัดลอกจาก .env.example)
cp .env.example .env
  1. แก้ไขไฟล์ .env เพื่อใส่ค่า API key ของ Anthropic และการตั้งค่าอื่นๆ ตามต้องการ

การใช้งาน

การพัฒนาในเครื่องเดียวกัน

เริ่มเซิร์ฟเวอร์ในโหมดพัฒนา (มีการโหลดซ้ำอัตโนมัติ):

npm run dev

การรันในโหมดผลิต

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

npm run build

เริ่มเซิร์ฟเวอร์:

npm start

การใช้งานผ่าน npx

npx mcp-p-anthropic-api-server

API Endpoints

  • GET /api/health - ตรวจสอบสถานะ API
  • POST /api/messages - ส่งข้อความไปยัง Claude AI
  • GET /api/tools - รายการเครื่องมือที่มีให้ใช้งาน
  • POST /api/tools/execute - ใช้งานเครื่องมือเฉพาะ

ตัวอย่างการใช้งาน API

การส่งข้อความไปยัง Claude AI

curl -X POST http://localhost:3000/api/messages \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-3-7-sonnet-20250219",
    "messages": [
      {"role": "user", "content": "Hello, Claude!"}
    ]
  }'

การใช้งานเครื่องมือ getCurrentTime

curl -X POST http://localhost:3000/api/tools/execute \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "getCurrentTime",
    "parameters": {"timezone": "Asia/Bangkok"}
  }'

การเรียกใช้ Anthropic API โดยตรง

โปรเจกต์นี้ได้เพิ่มตัวอย่างการเรียกใช้ Anthropic API โดยตรงผ่าน curl ในโฟลเดอร์ examples:

node examples/direct-anthropic-api.js "ข้อความที่ต้องการส่งไปยัง Claude"

ดูข้อมูลเพิ่มเติมได้ที่ examples/README.md

การทดสอบ

รันการทดสอบทั้งหมด

npm test

รันการทดสอบเฉพาะการตรวจสอบข้อมูล

npm run test:validation

รันการทดสอบเชิงบูรณาการ

npm run test:integration

รันการทดสอบพร้อมรายงานความครอบคลุม

npm run test:coverage

การใช้งานกับ Claude Desktop

คุณสามารถใช้โปรเจกต์นี้กับ Claude Desktop ได้โดยสร้างไฟล์ claude_desktop_config.json ดังนี้:

{
  "mcpServers": {
    "anthropic-api": {
      "command": "npx",
      "args": ["mcp-p-anthropic-api-server"],
      "env": {
        "ANTHROPIC_API_KEY": "sk-ant-xxxxx",
        "MODEL": "claude-3-7-sonnet-20250219"
      }
    }
  }
}

หลังจากตั้งค่า Claude Desktop:

  1. เปิด Claude Desktop
  2. ไปที่ Settings > Extensions
  3. ระบุพาธของไฟล์ claude_desktop_config.json
  4. บันทึกและรีสตาร์ท

เมื่อใช้งาน ให้เริ่มต้นคำขอด้วย @anthropic-api ตามด้วยข้อความของคุณ:

@anthropic-api ช่วยเขียนบทความเกี่ยวกับปัญญาประดิษฐ์

การแก้ไขปัญหาทั่วไป

การเชื่อมต่อกับ Anthropic API ไม่สำเร็จ

  1. ตรวจสอบว่าค่า ANTHROPIC_API_KEY ในไฟล์ .env ถูกต้อง
  2. ตรวจสอบการเชื่อมต่ออินเทอร์เน็ต
  3. ทดลองใช้ตัวอย่างการเรียกใช้ API โดยตรงใน examples/direct-anthropic-api.js

TypeError: app.address is not a function

หากพบข้อผิดพลาดนี้เมื่อรันการทดสอบด้วย supertest ให้พิจารณาวิธีการแก้ไขต่อไปนี้:

  1. ใช้การทดสอบแบบ direct mock แทน (ดูตัวอย่างจาก api-coverage-fixed.test.ts)
  2. ตรวจสอบว่า app เป็น Express application จริงๆ
  3. ใช้วิธีดักจับ route handlers โดยตรงแทนการใช้ HTTP requests จำลอง

การเชื่อมต่อกับ Claude Desktop ไม่สำเร็จ

  1. ตรวจสอบว่า Claude Desktop เวอร์ชันล่าสุด
  2. รีสตาร์ท Claude Desktop หลังจากแก้ไขการตั้งค่า
  3. ตรวจสอบว่า API key ในไฟล์ claude_desktop_config.json ถูกต้อง

การปรับแต่งและการขยายเพิ่มเติม

การเพิ่มเครื่องมือใหม่

เพิ่มเครื่องมือใหม่ได้โดยแก้ไขไฟล์ src/services/tools.ts:

// ตัวอย่างการเพิ่มเครื่องมือใหม่
registerTool('getWeather', 
  {
    type: 'object',
    properties: {
      location: { type: 'string', description: 'สถานที่ เช่น Bangkok, Thailand' }
    },
    required: ['location']
  },
  async (parameters) => {
    // ตรวจสอบพารามิเตอร์
    const { location } = parameters;
    
    // ดึงข้อมูลสภาพอากาศ (ตัวอย่าง)
    return { 
      temperature: 30, 
      condition: 'sunny', 
      location 
    };
  }
);

การปรับแต่งรูปแบบการบันทึกข้อมูล

แก้ไขไฟล์ src/logger.ts เพื่อปรับแต่งรูปแบบการบันทึกข้อมูล:

// เพิ่มหรือแก้ไข format ของ logger
const customFormat = winston.format.printf(({ level, message, timestamp, ...meta }) => {
  return `${timestamp} [${level.toUpperCase()}]: ${message} ${
    Object.keys(meta).length ? JSON.stringify(meta) : ''
  }`;
});

ใบอนุญาต

โปรเจกต์นี้ได้รับอนุญาตภายใต้ MIT License - ดูไฟล์ LICENSE สำหรับรายละเอียด

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Tavily Mcp
WindsurfThe new purpose-built IDE to harness magic
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Amap Maps高德地图官方 MCP Server
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Playwright McpPlaywright MCP server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
ChatWiseThe second fastest AI chatbot™
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
CursorThe AI Code Editor
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
DeepChatYour AI Partner on Desktop
Serper MCP ServerA Serper MCP Server