Skip to main content

A simple MCP server implementation using stdio protocol

Project description

MCP Python Stdio 示例项目

这是一个可分发的 Model Context Protocol (MCP) 服务端示例,采用了 stdio 协议进行通讯。

目录结构

项目采用了标准推荐的 src 布局:

  • src/stdio_python_demo/: 源代码目录
  • pyproject.toml: 包含打包元数据和 entry points 配置

如何作为用户直接运行

如果你将此项目上传到 GitHub,其他用户无需手动下载代码,可以直接通过 uvx 运行:

# 替换为你的 GitHub 仓库地址
uvx --from git+https://github.com/YOUR_USERNAME/stdio-python-demo.git stdio-python-demo

本地开发与运行

1. 安装环境

uv sync

2. 运行服务端

项目配置了 stdio-python-demo 入口点,可以直接运行:

uv run stdio-python-demo

3. 测试验证

运行模拟客户端脚本:

uv run test_client.py

或者使用 MCP Inspector:

npx @modelcontextprotocol/inspector uv run stdio-python-demo

分发到公共仓库 (PyPI) 的建议

如果你想让用户直接通过 uvx stdio-python-demo 运行:

  1. 注册 PyPI 账号。
  2. 在项目根目录运行 uv build
  3. 使用 uv publish 将构建好的包发布。

发布后,用户的运行体验将非常顺滑:

uvx stdio-python-demo
{
  "mcpServers": {
    "python-calc": {
      "command": "uvx",
      "args": [
        "stdio-python-demo"
      ],
      "env": {
        "GREETING_PREFIX": "你好"
      }
    }
  }
}
{
  "mcpServers": {
    "python-calc": {
      "command": "uv",
      "args": [
        "--directory",
        "D:/payegis/mcp/base-demo/stdio-python-demo",
        "run",
        "stdio-python-demo"
      ],
      "env": {
        "GREETING_PREFIX": "你好"
      }
    }
  }
}

环境变量配置示例

本项目演示了两种配置环境变量的方式:

1. 使用 .env 文件(本地开发推荐)

在项目根目录下创建 .env 文件,你可以配置自定义的问候语前缀:

GREETING_PREFIX="你好"

服务端在初始化时会调用 load_dotenv(),你可以通过 say_greeting 工具来验证配置是否生效。

2. 在 Claude Desktop 中直接配置

如果你在 Claude Desktop 中使用此项目,可以在配置文件的 env 字段中注入环境变量:

{
  "mcpServers": {
    "python-calc": {
      "command": "uv",
      "args": [
        "--directory",
        "D:/payegis/mcp/base-demo/stdio-python-demo",
        "run",
        "stdio-python-demo"
      ],
      "env": {
        "GREETING_PREFIX": "尊敬的"
      }
    }
  }
}

3. 可用工具演示

  • add_numbers(a, b): 基础数值加法。
  • say_greeting(name): 演示从环境变量获取前缀并拼接姓名。

4. 安全建议

  • 不要提交 .env:敏感信息(如 API Key)应仅存在于本地 .env 中,本项目已将其加入 .gitignore
  • 配置模板:参考 .env.example 创建你自己的本地配置。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stdio_python_demo-0.1.2.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stdio_python_demo-0.1.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file stdio_python_demo-0.1.2.tar.gz.

File metadata

  • Download URL: stdio_python_demo-0.1.2.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for stdio_python_demo-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2259650c6c4b8830444246fba8f734b19fa03e3607556c787fa1e59445830413
MD5 1c281d26462cc6e7e469f215a407b034
BLAKE2b-256 39074a9a157d9a61c308c4721fbc9629979613a4d2e0053194efe7d3d1692f40

See more details on using hashes here.

File details

Details for the file stdio_python_demo-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: stdio_python_demo-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for stdio_python_demo-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 113178f83d5d532e486d33909550ee3c67825d66a1137585a558dfe8ffb4ffa1
MD5 d66af6adf719d40c4ce2f582e5c40451
BLAKE2b-256 f0f2bfc005d806a0ee9445c2f2450b3272bb4a4f474245c792378e6567a8f917

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page