电影票务MCP服务器

一个为AI助手提供电影票务信息的MCP服务器,充当AI客户端与后端电影票务API之间的桥梁。

  • by cloudz-modern-platform-demo
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 cloudz-modern-platform-demo/movie-ticketing-mcp-server

使用说明

电影票务MCP服务器

MCP(模型上下文协议)服务器,为Claude Code等AI助手提供电影票务信息。该服务器充当人工智能客户端和后端电影票务API之间的桥梁。

需求

  • Python 3.12+ (必填)
  • 紫外线 (Python包安装程序)
  • 后端API服务器运行于 http://localhost:9000 (或通过配置 .env)

快速开始

1.安装uv

如果你没有 uv 尚未安装:

macOS/Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh

窗户:

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

或者通过pip安装:

pip install uv

2.安装依赖项

克隆存储库并安装依赖项:

# Navigate to project directory
cd movie-ticketing-mcp-server

# Install dependencies with uv (automatically creates virtual environment)
uv sync

这将:

  • 在中创建虚拟环境 .venv/
  • 从安装所有依赖项 pyproject.toml
  • 以可编辑模式安装软件包

3.配置环境

复制示例环境文件并根据需要进行配置:

cp .env.example .env

编辑 .env 要配置服务器设置,请执行以下操作:

# MCP Server Configuration
MCP_NAME="movie-ticketing-mcp-server"
MCP_HOST="0.0.0.0"
MCP_PORT=9100

# Backend API Server Configuration
API_SERVER_URL="http://localhost:9000"
API_ROOT_PATH="/api/v1"
API_OPENAPI_PATH="/openapi.json"

4.运行服务器

启动MCP服务器:

# Activate virtual environment first (if not already activated)
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Run the server
uv run movie-ticketing-mcp-server
or 
movie-ticketing-mcp-server

服务器将于启动 http://localhost:9100 并在以下位置暴露MCP端点 http://localhost:9100/mcp.

添加新工具

要添加新的MCP工具,请编辑 src/movie_ticketing_mcp_server/server.py:

@mcp.tool(
    name="your_tool_name",
    description="Description for AI clients",
    structured_output=True,
)
async def your_tool_name(param: str) -> CallToolResult:
    """Tool function docstring."""
    try:
        result = await http_client.get(
            f"{api_server_settings.root_path}/endpoint",
            params={"key": param}
        )

        return CallToolResult(
            content=[TextContent(type="text", text=f"{result}")],
            structuredContent=result,
        )
    except Exception as e:
        logger.error(f"Error: {e}")
        return CallToolResult(
            content=[TextContent(type="text", text=f"Error: {e}")],
        )

配置

MCP服务器设置

通过环境变量进行配置 MCP_ 前缀:

  • MCP_NAME:服务器名称(默认:“电影票务mcp服务器”)
  • MCP_HOST:服务器主机(默认值:“0.0.0.0”)
  • MCP_PORT:服务器端口(默认值:9100)

后端API设置

通过环境变量进行配置 API_ 前缀:

  • API_SERVER_URL:后端API基本URL(默认值:“http://localhost:9000")
  • API_ROOT_PATH:API根路径(默认值:“/API/v1”)
  • API_OPENAPI_PATH:OpenAPI规范路径(默认:“/OpenAPI.json”)

测试

测试MCP服务器连接

服务器已配置为与Cursor/Claude代码一起使用 .cursor/mcp.json:

{
  "mcpServers": {
    "movie-ticketing-mcp-server": {
      "url": "http://localhost:9100/mcp"
    }
  }
}