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
Visit Server

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.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured