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

Uploaded CPython 3.13Windows x86-64

mcp_methods-0.3.17-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.17-cp313-cp313-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

mcp_methods-0.3.17-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.17-cp312-cp312-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

mcp_methods-0.3.17-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.17-cp311-cp311-macosx_11_0_arm64.whl (2.8 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.17-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.17-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.17-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 748115360696f66638f0650dcb1bb208735e23adc3add6e4ee2b517e0289811d
MD5 b64ee89375127e558b7ce7434c5c3a18
BLAKE2b-256 0a72d478d7def4adb4a8312eb3012d6cfa4aadff534eae451b7e5a8d992de695

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 caff2f83b7360e684dd768bdb735b2af3e8afbfd8d1b99b5dd969749fa5a9a18
MD5 1300ed9ceadc9f1265af028fa7fe1df7
BLAKE2b-256 4f52455f104e75c1c54cb950b02b2ab3e3b2047f1df65bc9492c1a887dfdb049

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b65dae0d4aaf3a38154172fb4baed6c56d282048d5232cb89e29af5a86fe024f
MD5 e311a9366d4e8a52815ac734c4eebb0c
BLAKE2b-256 c834d65a7f30024d098589c13347a86bc3a24ef2e0648a40c79a78ebc63feacd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5268f11527fdb5c57103d3f0264334cabd283b982290c435510fbe661822dcbb
MD5 06b5950983770659b0b9fe94730d0b55
BLAKE2b-256 fb29909f529970ff756013d2ecf2f8b5bc0dd33533f9cae02a4247a00f358123

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c1c08edac965a1ed8202c13e0889866d06af66afbcb706ceef5667207844e6e5
MD5 c2066161cd495f9b5cdfc99acd59efe6
BLAKE2b-256 8a8f3c0ce150a4a69e44107b6ae070ec13a64781ec08a900bfac339855c52e5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f2159b807cba228dd7e322693442a71cd2b49fce5457e6afaaa63451dcc56f73
MD5 3b5cc9e2326e0cdafa999229a157764f
BLAKE2b-256 92c9fc40ab39803df8a0b44a9293668e7f913decc07704086e6fcebad6d37b59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c14e8031b6d0bdba6aa5d42b53ec4cc058445082f8ab1f0c1c7fb60670bdc21b
MD5 18076834c10a5fb1367fc6faed445e76
BLAKE2b-256 881d337a7fb2ad6e504e620fd511c6499ed8fa3a78ad0d91264507479f909421

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 1fd2cbf22e20acbfce239fc9314ea8b6b7dc1e0aaa23e2bc55c208673334d394
MD5 4408e2ce2ee23246b741ab725e3a862b
BLAKE2b-256 b5643c3fd550a98be48b3ea0ada6d7a44bba40cb88b1b3c99c0dc4c1f5d7dfed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5dbdc2c62dde99ab6e63fdadcc2447c82c974650d851ba1ada2fc132c9835cf3
MD5 890a9c48e960cbe2334cca9aefdc17a2
BLAKE2b-256 b32ef3aabfaa5bcc1f6be8176d4cc306e8124acf4df119004a6b11c69917dba9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3ecf07429b80a70157be0fbe55315469edae953b6c39827caf5ddeb1a00c093c
MD5 d2577fee657732f1336addc1fb62e710
BLAKE2b-256 b7ff3fac1e2aa6ba1f7d500204147c088e643ccfeb9821f48f67295cae4c288c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3edca4fc3c0a60a3373ad57887fe9c6dcf852ac7edb445f155135afe49d3453e
MD5 f577800bfe3c5a9b227a6d2d5bc55294
BLAKE2b-256 ea6b29177fa059a7c6d31822d5defb1c1c3e563cffa06275db6ee241adbc4a64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.17-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 57edb725647ea9a2b3d5bac54566450e2a3ecc80ba049803dcb9024ab3e6496b
MD5 271318ba672318c7b018dc2b1d71f5e6
BLAKE2b-256 982c33a60c79c7f6ce89ef156755d117620fba9dd1b2134f9920d5129323dcf3

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