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
ElementCache Drill-down cache for collapsed elements (code blocks, comments, patches, thread segments) in GitHub discussions
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

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_discussion("owner/repo", 123)

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

# Force re-fetch when discussion has changed
text = cache.fetch_discussion("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"])

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.14-cp313-cp313-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.13Windows x86-64

mcp_methods-0.3.14-cp313-cp313-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

mcp_methods-0.3.14-cp312-cp312-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.14-cp311-cp311-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.14-cp310-cp310-manylinux_2_39_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

mcp_methods-0.3.14-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.14-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f2c998d1fb386c06ccb6b87009142ed028e973e9fb0b0ccd09645d599314930c
MD5 6f80a88b75e57efe15bded470a10fa5e
BLAKE2b-256 35fb90b8eff165cf274e529a892797da5da5a16c7e26d874028c30a73473f900

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 0e2a3eabc55155b18bfec30dda3d2ff372a4d758172266255f80d616ab3577d3
MD5 3645e39689bd85f20fd79fe967f86684
BLAKE2b-256 b4f00eb4d4bd69aad7cebfd7b3da63d9df33bda9fb2ad80dc8ac5f3f64f57be1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0649a816e68abbb4a6652e9b984250a48307658f4a5820cf14a0e40b80deb462
MD5 ea18e4097c4462af64e87439d4ed5b7f
BLAKE2b-256 5a4d71e1149725d9935c3a25c179f9ee6851799fa2d6aaa7ccf025b2acfae9cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 34bac2123ab8a44de950dc8ce7adf2fa3b97073b1c4d5b9e2f7504dd4bb9500a
MD5 97e059c91fc1c0608c3eeab67e21e7a7
BLAKE2b-256 ca50f390b89a23747eeecb13a1df3adbc79d9ad38f4c8f36da3f45adef30440d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 efd2f24a3c29a9823fadc3eb17bb73a7b47b7c004452745f7616c0910336bcf1
MD5 95f11ab4e5e551b95dfe916fb80b8994
BLAKE2b-256 500a5cddf6445807879806e6c7485e53cd25c3165a362d6710dfb6d2db2eff1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f2787ce2d5708bf135af9e6d443ec96893fef6a58dec7d328b39364444d0a0a4
MD5 02d3b75856bfc522a9c2ae003eda59ea
BLAKE2b-256 2be05b052b0da7391ac3a6816b9d0d6daf6f1640b3dc7a9172269b92232934ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 dcb70234cdb2a10511749ccf7de13221c6128f45471c554cc16aee267aea708e
MD5 4dad9f53af3bee8ee61184f3d7d64031
BLAKE2b-256 d12d2498f24a8618ec0d67cccf89ef6deed81bf09a582c612e57177b29d40ca2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 9452637a643e3f6a1afde34758f0a96fee36f7dff945edf27d346933864c5c9b
MD5 8e3023b9caa8e110ca0e1fc65e2c0944
BLAKE2b-256 d2c76ebb84d0959daf4fed70a0c27f7b576aa6ffe3d3bdb2dcd221ef0c5c2cd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 144e488b364afa8e9111b8d03e4b5c1364535300576c9194214b95f5927434e3
MD5 f03b8d4bf30652a0e7418f66efc0b34f
BLAKE2b-256 f2ac7af63be45e829e2529c2ac0ade235c038511d233612b7022045c5d919bc0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 348a279db76bd269df06d167cbc39138b2c32630b50d70885e30301b6429e03c
MD5 18162eb50e26c3bb9f13ad1602c553a0
BLAKE2b-256 548491c49c3b6a54d03da5d8cafffaa8f2053171dfef13d46c1887e96bd82460

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 625358fb25dfc7deb717fc036c41da609c5ea1119671d90c5ba6060644a243f8
MD5 522a5c3b38a6121f106739b652fb977f
BLAKE2b-256 8f8a19064388f74a027c32583e3e49fd28783946b80298e4e5da160e8b3520b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.14-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 baebc166901e6c8ae217e6720cb53d9ff0065572cff4f412c629c1ab76f05130
MD5 baf4cb20d68dcbce478162fc0150b671
BLAKE2b-256 73460fbf1d3aa2ac2938da6a7346dca4f5451bd76c2afc4651bf5b755bfc7e62

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