mcp-shell-server-example

mcp-shell-server-example

An educational MCP server exposing shell command execution (PowerShell and sh) and a benign tool for learning about MCP tools, resources, and security risks like tool poisoning.

Category
访问服务器

README

mcp-shell-server-example

Follow on X WhatsApp Channel License

A small, educational Model Context Protocol (MCP) server built with the official Python SDK.

The goal of this repo is not to be a production tool — it's a minimal, readable reference for people learning MCP: how a server exposes tools and resources, how a client (like an AI agent) discovers and calls them, and how to run/test the whole thing locally or in Docker.

What is MCP?

The Model Context Protocol is an open standard that lets AI applications (like Claude) connect to external systems in a consistent way. An MCP server exposes capabilities — mainly:

  • Tools — functions the AI can call (e.g. "run this shell command", "fetch this URL")
  • Resources — data the AI can read (e.g. a file's contents)

An MCP client (built into an AI app, or a debugging tool like MCP Inspector) connects to the server, discovers what it offers, and calls it on the AI's behalf. Communication happens over a transport — this server uses stdio (standard input/output), the simplest option: the client launches the server as a subprocess and talks to it over its stdin/stdout using JSON-RPC.

Architecture

flowchart LR
    subgraph Client
        A[MCP Client<br/>Claude Code / MCP Inspector]
    end

    subgraph Server["mcp-shell-server-example (stdio)"]
        B[FastMCP server.py]
        T1[terminal<br/>PowerShell]
        T2[terminal_linux<br/>sh]
        T3[benign_tool<br/>curl fetch]
        R1[mcpreadme<br/>resource]
    end

    A <-- "JSON-RPC over stdio" --> B
    B --> T1
    B --> T2
    B --> T3
    B --> R1
    T1 -.-> H[(Host OS)]
    T2 -.-> C[(Container OS)]
    T3 -.-> G[(Remote gist)]

The client launches server.py (directly with uv, or inside a Docker container) as a child process and exchanges MCP messages with it over stdio — no network port required.

What this server exposes

Name Type Description
terminal Tool Runs a command via PowerShell on the host. Meant for local/Windows use.
terminal_linux Tool Runs a command via /bin/sh. Meant for use inside the Docker container.
benign_tool Tool Downloads content from a fixed URL with curl and returns it. Included as a deliberate demo of a real MCP risk: a tool with an innocuous name that pulls in remote, untrusted content — a pattern known as tool poisoning / indirect prompt injection. Useful for learning to think critically about what MCP tools actually do, not just what they're named.
mcpreadme Resource Returns the contents of mcpreadme.md.

⚠️ terminal / terminal_linux run arbitrary shell commands with no sandboxing or allowlist. That's intentional for a learning project, but treat this as a local playground, not something to expose to untrusted clients or the network.

Prerequisites

  • Python 3.12+
  • uv — used to manage the virtual environment and run the server
  • (Optional) Docker — to run the server in a container
  • (Optional) Node.js — needed to run MCP Inspector via npx

Running locally

git clone https://github.com/mohamedelamraoui1/mcp-shell-server-example.git
cd mcp-shell-server-example
uv sync
uv run server.py

The server then waits on stdio for an MCP client to connect — this is normal, it won't print anything and won't respond to plain typed text (it only understands JSON-RPC).

Running in Docker

docker build -t shell-server-app .
docker run -i --rm shell-server-app

Inside the container, use terminal_linux instead of terminal — PowerShell isn't installed in the (Debian-based) image.

Testing with MCP Inspector

MCP Inspector is a web UI for manually calling a server's tools/resources without needing a full AI client.

Against the local server:

uv run mcp dev server.py

Against the Docker image:

npx @modelcontextprotocol/inspector docker run -i --rm shell-server-app

Either command prints a local URL — open it, click Connect, then use the Tools tab to call terminal / terminal_linux / benign_tool, or the Resources tab to read mcpreadme.

Example: get "HELLO MCP" printed back via the terminal tool by passing this as the command argument:

echo " _   _      _ _       __  __  ____ ____  "
echo "| | | | ___| | | ___  |  \/  |/ ___|  _ \ "
echo "| |_| |/ _ \ | |/ _ \ | |\/| | |   | |_) |"
echo "|  _  |  __/ | | (_) || |  | | |___|  __/ "
echo "|_| |_|\___|_|_|\___/ |_|  |_|\____|_|    "

Connecting it to Claude Code (real-world usage)

This server was built and tested end-to-end using Claude Code as the MCP client — including scaffolding the server itself, containerizing it, and then wiring it back in as a live tool Claude Code could call. That's a good demonstration of the MCP loop in practice: an AI agent using a protocol-standard interface to run real commands and fetch real data, instead of a one-off custom integration.

To connect it yourself, add a .mcp.json file at your project root (Claude Code loads this automatically and asks you to approve it on startup):

{
  "mcpServers": {
    "m-shell-server": {
      "command": "uv",
      "args": ["run", "--directory", "/absolute/path/to/mcp-shell-server-example", "server.py"]
    }
  }
}

Restart Claude Code in that directory, approve the server when prompted, then check it's connected with /mcp. From then on, Claude Code can call terminal, terminal_linux, benign_tool, and read the mcpreadme resource directly as part of answering your prompts.

Project structure

.
├── server.py         # the MCP server: tools + resource definitions
├── mcpreadme.md       # content exposed via the mcpreadme resource
├── pyproject.toml     # project metadata + dependencies (managed by uv)
├── uv.lock            # locked dependency versions
├── Dockerfile          # container build, following uv's official Docker guide
├── .dockerignore
└── .mcp.json           # example Claude Code project-scoped MCP config

References

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选