SWAPI MCP Server

SWAPI MCP Server

Enables LLMs and clients to search for Star Wars characters, planets, and films by exposing SWAPI as MCP tools.

Category
访问服务器

README

SWAPI MCP Server

A Model Context Protocol (MCP) server that wraps the Star Wars API (SWAPI) as MCP tools, allowing LLMs and clients to search for Star Wars characters, planets, and films.

Features

  • MCP-compliant server using the official TypeScript SDK
  • Exposes three tools:
    • search_character: Search for a Star Wars character by name
    • get_planet: Get detailed planet info by ID
    • get_film: Get detailed film info by ID
  • Supports both HTTP (stateless, streamable) and stdio transports

Code Structure

  • index.ts: Main entry point. Sets up the MCP server, registers tools, and configures HTTP and stdio transports.
  • test-client/: Example OpenAI client that demonstrates how to call the MCP server as a tool from an LLM.

Tool Details

search_character

  • Input: { name: string }
  • Description: Searches SWAPI for characters matching the given name.
  • Returns: JSON-formatted list of matching characters.

get_planet

  • Input: { id: string }
  • Description: Fetches detailed info for a planet by its SWAPI ID.
  • Returns: JSON-formatted planet details.

get_film

  • Input: { id: string }
  • Description: Fetches detailed info for a film by its SWAPI ID.
  • Returns: JSON-formatted film details.

How It Works

  • Uses the @modelcontextprotocol/sdk to create an MCP server.
  • Each tool is registered with an input schema (using zod) and an async handler that fetches data from SWAPI.
  • The server can be accessed via HTTP POST requests to /mcp or via stdio (for CLI clients).

Running the Server

Prerequisites

  • Node.js v18 or newer
  • npm

Install dependencies

npm install

Run in HTTP mode (default)

npx ts-node index.ts
  • The server will listen on http://localhost:3000/mcp (or the port set in the PORT environment variable).

Run in stdio mode (for CLI clients)

  • The server automatically starts a stdio transport for CLI-based MCP clients.

Example HTTP Request

List tools:

curl -X POST http://localhost:3000/mcp \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":0,"method":"tools/list","params":{}}'

Search for a character:

curl -N -X POST http://localhost:3000/mcp \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "search_character",
      "arguments": { "name": "Luke" }
    },
    "id": 1
  }'

Get a planet by ID:

curl -N -X POST http://localhost:3000/mcp \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "get_planet",
      "arguments": { "id": "1" }
    },
    "id": 2
  }'

Get a film by ID:

curl -N -X POST http://localhost:3000/mcp \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "get_film",
      "arguments": { "id": "1" }
    },
    "id": 3
  }'

Test Client: OpenAI + MCP Integration

The test-client/ directory contains an example script (swapi-client.ts) that demonstrates how to call the MCP server as a tool from an OpenAI LLM (e.g., GPT-4o-mini).

How it works

  • Uses the OpenAI SDK and MCP tool integration.
  • Registers the MCP server as a tool for the LLM.
  • Sends a prompt/question to the LLM, which can call the MCP tools to answer.

Example: test-client/swapi-client.ts

// node --loader ts-node/esm ./swapi-client.ts
import 'dotenv/config';
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });

const tools = [
  {
    type: "mcp" as const,
    server_label: 'swapi',
    server_url: 'http://localhost:3000/mcp', // this has to be accessible from your LLM
    require_approval: 'never' as const,
  },
];

const resp = await openai.responses.create({
  model: 'gpt-4o-mini',
  tools,
  input: 'Where was Luke Skywalker born and how tall is he?',
});

console.log(resp.output_text);

Running the test client

  1. Install dependencies:
    cd test-client
    npm install
    
  2. Set your OpenAI API key in a .env file:
    OPENAI_API_KEY=sk-...
    
  3. Start the MCP server (in the parent directory):
    npx ts-node index.ts
    
  4. Run the test client:
    node --loader ts-node/esm ./swapi-client.ts
    
  • You should see the LLM's answer, which may include information fetched from the SWAPI MCP tools.
  • You can also point the server_url to a public ngrok URL if you want to test from outside localhost.

Notes

  • The server is stateless for HTTP requests (no session management).
  • CORS is enabled for the mcp-session-id header.
  • For more details on MCP, see the TypeScript SDK documentation.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选