Skip to content

About

Write Model Context Protocol servers in pure Bash. One vendorable file: JSON-RPC 2.0 stdio loop, tool dispatch, inputSchema validation, logging.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

52 Commits

Folders and files

Repository files navigation

bash-mcp-sdk

bash-mcp-sdk

CI bash 5.1+ jq 1.7+ License: MIT

A Bash framework for writing Model Context Protocol servers. It handles the JSON-RPC 2.0 stdio loop, tool dispatch, argument validation against each tool's inputSchema, and logging.

One file, lib/mcpserver_core.sh. It sources nothing and needs jq, plus ps and mkfifo for the tool lifecycle. What a server script can rely on and has to do is in docs/server-contract.md. The runtime mechanism behind those contracts is in docs/architecture.md.

📌 Requirements

  • Bash 5.1+ — sourcing the file under an older Bash is refused.
  • jq 1.7+ — below that floor, jq parses every number to a double. The validator's integer check then cannot see a fraction the double rounded away.
  • ps and mkfifo — the tool lifecycle. run_mcp_server creates the lifeline with mkfifo as it starts, and ps is reached only once a tool runs. BusyBox ships both, so an Alpine consumer needs no procps (why the code reads a full ps listing is in docs/architecture.md §Cancellation).
  • /dev/stdin — the SDK reads a tool result's text through it. On Linux it is a link into /proc.

Sourcing the file checks the first two floors. A Bash below 5.1, a jq that is missing or cannot run, or a jq below 1.7 is refused on stderr. The refusal names the requirement and, where there is one, the version found. It adds remediation for the platform: its package manager's command where one can be determined, and a generic line otherwise.

Note

macOS ships Bash 3.2. Install a current Bash (brew install bash) or run servers under one. An MCP host launched from the desktop does not read your shell profile. The newer Bash therefore has to sit on the PATH that host starts the server with.

Where to set that PATH, and why ps and mkfifo go unchecked: Dependencies and PATH.

📦 Installation

There is no install step. Copy lib/mcpserver_core.sh into your project and source it. See Vendoring for keeping the copy current.

🗜️ API

Function Purpose
run_mcp_server The stdio read loop. Call it last; it returns when stdin closes.
process_request Parse and route one JSON-RPC line. Useful for testing a server.
handle_initialize The initialize handler process_request routes to. Answers protocolVersion, serverInfo and capabilities.
handle_tools_list The tools/list handler process_request routes to. Answers the tools array.
handle_tools_call The tools/call handler process_request routes to. A consumer can drive it directly to dispatch one call without the read loop.
validate_tool_arguments Check a call's arguments against the tool's inputSchema. Rejects a tool that the tools list does not declare exactly once with a non-null inputSchema, whose inputSchema uses a keyword the validator does not enforce, or whose inputSchema holds a keyword of the wrong type, such as "required": false.
create_response Build a JSON-RPC result envelope.
create_error_response Build a JSON-RPC error envelope. Optional 4th arg data (JSON value) is included when non-empty.
log Append to MCP_LOG_FILE, and to MCP_EXTRA_LOG_FILE when set. A write that fails leaves the server running and prints mcp: cannot write log file <path> to stderr. Newlines and carriage returns in the message are written as \n and \r.
read_json_file Read one JSON document from a file. Prints it when it is a JSON object; returns 1 on a missing file or one that does not hold exactly one JSON object.

Configured by environment variable before sourcing:

Variable Default Meaning
MCP_TOOLS_LIST_FILE tools.json The tools the server advertises, and their schemas.
MCP_CONFIG_FILE config.json protocolVersion, serverInfo, capabilities.
MCP_LOG_FILE /dev/null Where log writes.
MCP_EXTRA_LOG_FILE unset Second log target; PROJECT_ROOT resolves a relative path.
MCP_LOG_STDERR 0 Set to 1 to also mirror each log line to stderr.

Defined by the server script and resolved as shell functions only:

Function Called
tool_<name> For a tools/call naming <name>, when tools.json declares it. Receives the arguments JSON.
tool_<name>_cancel Optional. When a call of <name> is cancelled, or the server shuts down with it in flight.
mcp_before_tool_call Optional. Before every dispatched tool, with the tool name and the arguments JSON.

Set by the SDK for a call, not by the consumer:

Variable Seen by Meaning
MCP_CALL_TMPDIR mcp_before_tool_call, the tool function and what it starts, tool_<name>_cancel A directory private to the call, removed when the call ends. See Per-call files.

Methods handled: initialize, tools/list, tools/call and ping; the notifications notifications/initialized and notifications/cancelled. A request for any other method returns -32601; a notification for one is logged and ignored.

How a missing configuration file, an invalid id, a non-string method and a malformed line are answered: Configuration files and protocol handling.

Writing a server

Each tool is a Bash function named tool_<name>, receiving the call's arguments as one JSON string:

#!/usr/bin/env bash
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export MCP_TOOLS_LIST_FILE="${HERE}/tools.json"
export MCP_LOG_FILE="${HERE}/server.log"

source "${HERE}/mcpserver_core.sh"

tool_greet() {
    local args="$1"
    local name
    name=$(printf '%s' "$args" | jq -r '.name')
    printf 'Hello, %s\n' "$name"
}

run_mcp_server

The contract a server is written against is in docs/server-contract.md:

🔗 Vendoring

Consumers copy lib/mcpserver_core.sh into their own tree and pin the release they copied from. The recommended shape:

  1. A lock file recording the tag, e.g. .mcp-sdk.lock containing version=v1.0.0.
  2. A vendor script that downloads that tag and writes the file to every consuming path. A --check mode re-vendors to a temp directory and diffs instead of writing.
  3. A Renovate custom manager watching the lock file, so upgrades arrive as PRs:
{
  "customManagers": [
    {
      "customType": "regex",
      "managerFilePatterns": ["/^\\.mcp-sdk\\.lock$/"],
      "matchStrings": ["version=(?<currentValue>v\\d+\\.\\d+\\.\\d+)"],
      "depNameTemplate": "shopwareLabs/bash-mcp-sdk",
      "datasourceTemplate": "github-releases",
      "versioningTemplate": "semver"
    }
  ]
}
  1. A CI job on the Renovate branch. It runs the vendor script, pushes the refreshed file into the same PR, and gates every build on --check.

Why step 4 runs in CI rather than in Renovate: Keeping the vendored copy current.

🧩 Used by

Project MCP servers built on the SDK
shopwareLabs/ai-coding-tools Shopware development tooling for PHP, admin and storefront (PHPStan, PHPUnit, ESLint, Jest, Vitest), environment lifecycle, test-writing rules
it-bens-ai/clipboard-agent-tools Clipboard copy for AI coding agents, with native and SSH backends
shopwareLabs/github-agent-tools GitHub CLI access for AI coding agents, as a read server and an opt-in write server

Each project vendors lib/mcpserver_core.sh at a pinned tagged release. To list your project, open a PR that adds a row.

🧪 Testing

./.github/scripts/setup-bats.sh          # once, installs into .bats/
.bats/bats-core/bin/bats -r tests/

Or run it in containers, which need Docker:

./scripts/test-linux.sh            # ShellCheck, then Debian and Alpine
./scripts/test-linux.sh debian     # one distro: no ShellCheck
./scripts/test-linux.sh bash-5.1   # Alpine on the official bash:5.1 image

Set BASE_IMAGE_DEBIAN or BASE_IMAGE_ALPINE to override that build's base image. Set JQ_VERSION to install that upstream static jq release instead of the distro package. Set BATS_TEST_TIMEOUT to change the per-test timeout in seconds (default 90).

Debian is the glibc/GNU run. Alpine adds only Bash and jq to musl/busybox, so a suite that leans on a tool the dev machine happens to carry fails there. The bash-<major>.<minor> legs run the same suite on the official Bash images CI uses, 5.1 and 5.3, and fail when the image's Bash is not the named one. Debian and Alpine run their distro Bash, 5.2 at the default base images.

Every leg gates, in bare and named runs alike.

Lint with ShellCheck before pushing:

find lib tests scripts .github/scripts -type f \( -name '*.sh' -o -name '*.bats' -o -name '*.bash' \) \
  -exec shellcheck --shell=bash --format=gcc {} +

🚫 Not Supported

  • MCP resources and prompts — tools only (why they are not planned)
  • Transports other than stdio
  • Concurrent request handling; the server loop is strictly sequential
  • A request id, method or tool name larger than the OS command-line argument limit: the request can go unanswered and the server can exit with status 126
  • Running the tool command anywhere but the local shell. Container, VM, and remote execution are a consumer concern; this SDK dispatches to a Bash function and nothing more.

⚖️ License

MIT

About

Write Model Context Protocol servers in pure Bash. One vendorable file: JSON-RPC 2.0 stdio loop, tool dispatch, inputSchema validation, logging.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages