Skip to main content

Reusable utility methods for MCP servers

Project description

mcp-methods

Shared Rust-powered utilities for MCP servers. Pip-installable library that provides fast file search, GitHub integration, and text processing — the common building blocks needed when writing MCP tool servers.

Install

pip install mcp-methods

For development (requires Rust toolchain + maturin):

pip install -e ".[dev]"

What's included

Function Purpose
list_dir Tree-formatted directory listing with depth control, glob filtering, .gitignore support, dir summaries, and annotation callback
ripgrep_files Ripgrep-powered file search with parallel walking, early termination, context lines, and multiple output modes
ripgrep Drop-in replacement for the Claude Code Grep tool interface
read_file Safe file reading with path traversal protection and line range support
github_discussions Fetch a single issue/PR with smart compaction, or list issues/PRs with filters
git_api GitHub REST API wrapper with token auth
has_git_token Returns whether a usable GITHUB_TOKEN is reachable (used for honest tool listing)
ElementCache Drill-down cache for collapsed elements (code blocks, comments, patches, thread segments) in GitHub discussions
html_to_text Lightweight HTML → plain-text converter (markdown-flavoured)
ripgrep_lines Search through text lines with context window merging
ripgrep_json_fields Extract fields from JSON text
compact_discussion / compact_text / collapse_code_blocks Text compaction utilities
extract_github_refs Parse GitHub issue/PR references from text
detect_git_repo / validate_repo Git repository detection and validation
mcp_methods.fastmcp Composable tool registrations for FastMCP servers — see below

Python API

list_dir(path, *, depth=1, glob=None, dirs_only=False, relative_to=None, respect_gitignore=True, skip_dirs=None, include_size=False, annotate=None)

Tree-formatted directory listing.

from mcp_methods import list_dir

# Basic tree
tree = list_dir("/project/src", depth=2, glob="*.py", relative_to="/project")

# With annotation callback (e.g. loc from knowledge graph)
def get_loc(rel_path):
    node = graph.get_file(rel_path)
    return f"({node.loc} loc)" if node else None

tree = list_dir("/project/src", depth=2, annotate=get_loc)
# src/
# ├── main.py        (144 loc)
# ├── utils.py       (28 loc)
# └── models/
#     ├── user.py    (89 loc)
#     └── post.py    (112 loc)

ripgrep(pattern, *, path=".", glob="*", type=None, output_mode="files_with_matches", max_results=None, offset=0, ...)

Claude Code Grep-compatible interface.

from mcp_methods import ripgrep

results = ripgrep(r"def \w+", path="/project", type="py", max_results=50)

ripgrep_files(source_dirs, pattern, *, glob="*", type_filter=None, output_mode="content", max_results=None, offset=0, match_limit=None, relative_to=None, ...)

Full interface with multi-directory search. max_results limits output entries, match_limit caps the search engine for early termination.

from mcp_methods import ripgrep_files

results = ripgrep_files(
    ["/project"],
    r"def \w+",
    type_filter="py",
    relative_to="/project",
    match_limit=500,
    max_results=100,
)

github_discussions(*, repo=None, number=None, kind="all", state="open", sort="created", limit=20, labels=None)

Fetch a single discussion or list discussions.

from mcp_methods import github_discussions, ElementCache

# List open issues
issues = github_discussions(repo="owner/repo", kind="issue", state="open")

# List pull requests
prs = github_discussions(repo="owner/repo", kind="pr", limit=10)

# Fetch a single issue/PR with smart compaction
issue = github_discussions(repo="owner/repo", number=123)

ElementCache — progressive disclosure for GitHub discussions

Cache for drill-down into collapsed elements. Fetches a discussion once, then lets you explore code blocks, comments, and PR diffs without re-fetching.

from mcp_methods import ElementCache

cache = ElementCache()

# First call fetches from GitHub API, compacts, and caches elements
text = cache.fetch_issue("owner/repo", 123)

# Subsequent calls return cached summary (no network)
summary = cache.fetch_issue("owner/repo", 123)
# → "Cached owner/repo#123 — 5 elements available: cb_1, comment_2, patch_1, patch_2, patch_3"

# Force re-fetch when the issue has changed upstream
text = cache.fetch_issue("owner/repo", 123, refresh=True)

# Drill into a collapsed code block
code = cache.retrieve("owner/repo", 123, "cb_1")

# Drill into a PR patch with grep
result = cache.retrieve("owner/repo", 123, "patch_1", grep="error_handler")

# Drill into a patch with line range
result = cache.retrieve("owner/repo", 123, "patch_2", lines="10-30")

# List available elements
ids = cache.available("owner/repo", 123)

PR diffs are automatically collapsed into patch_N elements in the compact view. Each patch stores the filename, additions/deletions, and full diff text — supporting grep and line-range drill-down.

Large discussions (50+ comments) are automatically digested: first 5 + maintainer highlights + last 5 comments shown inline, with the full middle cached as individual comment_N elements and a searchable comments_middle segment.

git_api(repo, path, *, truncate_at=80000)

GitHub REST API wrapper. For comparing branches/tags, use compare:

from mcp_methods import git_api

# Compare two refs
diff = git_api("owner/repo", "compare/main...feature-branch")

# List commits
commits = git_api("owner/repo", "commits?per_page=10")

read_file(path, allowed_dirs, *, offset=0, limit=0, max_chars=0, transform=None)

Safe file reading with path traversal protection.

from mcp_methods import read_file

content = read_file("src/main.py", ["/project"])

mcp_methods.fastmcp — drop-in tools for FastMCP servers

If you're running your own FastMCP server but want the same tool surface the bundled mcp-server binary ships (source navigation, graph overview, Cypher with CSV export, save_graph), import these helpers and register them on your app:

from mcp.server.fastmcp import FastMCP
from mcp_methods.fastmcp import (
    register_overview,
    register_cypher_query,
    register_source_tools,
    register_save_graph,
    serve_csv_via_http,
)

app = FastMCP("My Server")
register_overview(app, graph, overview_prefix="My custom guidance")
register_cypher_query(app, graph, csv_dir="temp/")
register_source_tools(app, source_roots=["./source"])
register_save_graph(app, graph)
_server, base_url = serve_csv_via_http("temp/")  # optional CORS-enabled HTTP server
app.run(transport="stdio")

Each helper is a thin (~10-line) wrapper over the existing Rust PyO3 surface — there's no logic duplication between the YAML-driven binary and these helpers, so agent behaviour is identical regardless of which path booted the server. graph is any object exposing describe() / cypher() / save(); kglite's KnowledgeGraph satisfies it. A runnable end-to-end stub lives at examples/fastmcp_demo.py.

Deployment — mcp-server binary

This crate also ships an MCP server binary at crates/mcp-server. Build and install it with:

cargo install --path crates/mcp-server
# → ~/.cargo/bin/mcp-server

For deployments that previously pinned a binary path elsewhere (e.g. /opt/miniconda3/envs/embeddings/bin/kglite-mcp-server), drop a symlink:

ln -s ~/.cargo/bin/mcp-server /opt/miniconda3/envs/embeddings/bin/mcp-server

The binary is domain-agnostic — source tools + GitHub access + a manifest-driven tool surface. Downstream binaries (e.g. kglite-mcp-server) re-export mcp_server::McpServer::new(...) to layer graph-specific tools on top while reusing the boot sequence, .env loading, workspace mode, watch mode, and embedder lifecycle.

Operating modes (set via CLI flag or the YAML manifest):

Mode How to set When to use
bare no flag testing the protocol layer in isolation
source-root --source-root DIR or YAML source_root: fixed local directory; no clone
workspace (github) --workspace DIR clone-and-track GitHub repos
workspace (local) YAML workspace: { kind: local, root: ..., watch: ... } fixed local dir + optional file watcher; alternative to the legacy code_review server
watch --watch DIR rebuild downstream artifacts on file changes

YAML manifest declarations win over CLI flags when both are set (same precedence rule as source_root:).

Architecture

All heavy lifting is in Rust (PyO3/maturin), compiled to a native Python extension:

  • grep: Uses grep-regex, grep-searcher, and ignore crates directly (not a ripgrep subprocess). Parallel file walking with per-thread searcher reuse, mmap, SIMD literal optimization, and .gitignore support.
  • GitHub: HTTP via ureq, JSON processing via serde_json, text compaction in Rust. PR diffs are collapsed into cacheable elements for progressive disclosure.
  • File I/O: Path validation and traversal protection in Rust.

License

MIT

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

mcp_methods-0.3.22-cp313-cp313-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.13Windows x86-64

mcp_methods-0.3.22-cp313-cp313-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.22-cp313-cp313-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

mcp_methods-0.3.22-cp312-cp312-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.12Windows x86-64

mcp_methods-0.3.22-cp312-cp312-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.22-cp312-cp312-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

mcp_methods-0.3.22-cp311-cp311-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.22-cp311-cp311-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.22-cp311-cp311-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

mcp_methods-0.3.22-cp310-cp310-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.22-cp310-cp310-manylinux_2_39_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.22-cp310-cp310-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file mcp_methods-0.3.22-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f84f7fd01361a4b5051dbe38038d9682076bc6e361abe85afa30407beeeb5b4d
MD5 fd21db1cf0dd9ece01da18b7a57eef6c
BLAKE2b-256 5556b555ea2067227c9a6e162bf12978cb8550e1583b5502b900c6124ca29ada

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 0e7469399c841838e07a8b24cb19c10320e830b2a7d523877f889231e3a2cb90
MD5 be13a41cae18cfbea9930a1b9405cdb2
BLAKE2b-256 c55d2baa256cfd64fa0b48e2f2ae62565682d1b4d3c88005de0d98fa8833c53c

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ec5f87a434a5226ea7263f026397eee058e56cb66b052aa1201494da02054e8f
MD5 38ce65a934de4ffc247be406449139bd
BLAKE2b-256 cc6fa5e32e00a44338d68bc14f282ad425faddb0cf326e362048d1a10c89ab56

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0f72e5da5d0d74eee1b9202648f5864be0eacb9fb2d64d68a9ffd30a4bc275fa
MD5 aa5d891d0e32054963e32aaf4447b0eb
BLAKE2b-256 1b0934fabb730c25f28557d6e6aa107e806e4d55f306d7ffe44228013a2fe6f5

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 13e1246f0747476fd9212356d3f4af662cd0c37bba54c0d5abd75d4b4ee49767
MD5 7a8a62b6e1fc53ccde04aa93377cd8ee
BLAKE2b-256 83c25ec3582e85cbff391a241c5ac105a57930213c12594906fa83d93f9e7f0f

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5dd2516ceb36fc7fcf7b903c68a2a41ccb97bc106c3409c5d27a27a99ab7c683
MD5 7d4fc7284c09488f4572f37b54a5da4b
BLAKE2b-256 1ac028a26c020936513aa432e49c4d8b254a6e8ba385a45496c407f4ca423c92

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8589e2d892c7d52636a3247d240d632bd7374605ef67056a425963d7e6592698
MD5 5e4c944daa17f53a423ca6352d17f1ac
BLAKE2b-256 362db6482a39d6f46d6d71caa7df61ebd6888690f5b26031199a0016c2695b63

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 6f74b50d3664bcdfdcbe440f88d90818159557566acf1f2dc3a67f526b0b6e9c
MD5 06bd184c1ffcfdcd2fa23e21c766d8a3
BLAKE2b-256 455cdd5ecff7755ee1f7564b67c7327242096c5b53873b45e5f5a588e0b0b9fb

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fb38d11d72a2b6d9a4dce70a8450d2f95ef3123fde4e755d76a5ea8537634b58
MD5 c05f2391bd1e3e5cb4e6fd3288f65386
BLAKE2b-256 51c76b979a055ec82d85c08439cce16ab6a1cd59ad88fc853b5a524cd2bf67fb

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9b1e888507d991c26c30b486b32b073de8ec20b48d4491272f8f41211653238d
MD5 31191d063482d1a0607b8eb8e075c712
BLAKE2b-256 191bf3f50f316dd6ebc8c1991481dff554be0329e09018c56f8bfaf70b0a52ab

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 09f2c191a3f6ffe19a027b69ba7903849896ab0cb1b748e14b087f28e9e9d49a
MD5 a39bc555b61a674dac0a528ab82360a0
BLAKE2b-256 8a02c030a86957913b38f384969df8e41f89d445ab02169e542c6cdb2f4ceed3

See more details on using hashes here.

File details

Details for the file mcp_methods-0.3.22-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.22-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 13e92001cdd3bf4d833e2449efcacd0b624e423195b3f9ec4f9851b1b34a76fa
MD5 027599c0083d3834562b75dbd032df6d
BLAKE2b-256 eb2cefb3de095a48d964185806c5228ca9f75b5cb1fecdceacd4a3a6089011b9

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