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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

mcp_methods-0.3.7-cp312-cp312-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

mcp_methods-0.3.7-cp311-cp311-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

mcp_methods-0.3.7-cp310-cp310-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dbc501a10ff7c62a406dae168ddd49675e250663491f2b8e83da0a35bb96e3d8
MD5 93aa690ebbb6281371c1d97519a43aed
BLAKE2b-256 ef74f02041a0a7c387814048b2714c2ed83d88b81acb1dcfb05871e09d876b6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c761e0e9123acfcb587938c6720e646f25f0170406032438daa672e3fe1af74c
MD5 c248a1173b4a2f5fb1733436913581f3
BLAKE2b-256 104bbe9ec39fdbba9382342fd3330a16fe36e6fe46fa465a3fb654f6924d7efa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b093e4b891277d31509733d0a8522af8caf0fb5ec576d101551a2dcad2f818f4
MD5 900ead1e1c930e0d9dd1ed714a4a50e1
BLAKE2b-256 4a3c6d387033de994af481bf1bd29edce253bf6e8f0520bc76965ccb43bda491

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7dea5b8d85f20e9e64443743d4c6ddf04af264d4a48d1716df31208657f3b047
MD5 622c8b08fceec58f2dd525f71170fda1
BLAKE2b-256 a9ad96f6fa3609f2677ea715e7d6509ee27d800ea74681c95848808a47f1e49c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 2c0fb0932893b4780f7fc32d77659479a09db574b09048eb701384e8366f8957
MD5 76c3af71503aeb7e3da9ad91811316f7
BLAKE2b-256 8d0da2ca2c7ce3cc32fe664c3738876ddf8648510a5acb8c626b0e1eaa352f56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 feb5879bc3ca484cde62d3f056d8c5656e190ce9543ddde98cf90a6991f73f85
MD5 9c006de72ed7264879b4c20e1eb902ff
BLAKE2b-256 27fdbf627dd0350be8d9e7bb992c819c86fa4b9ca1b0b1f3d4925d261cf06277

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0058323173bd9a2f08b94908441d212ed0d9570304050ff37853f5a8b8681e61
MD5 9259002e2b1951104670339c89b25c8e
BLAKE2b-256 4ec1bfc40c95efd333c93772a298405ebe876ec49c263916e17737ffe2529c22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3fea2293ba78cdfcfc54027fd2f8d1e13f3e9c78a256134f9bde08ce50a2d749
MD5 f72711647b85138b62c845880e5e6575
BLAKE2b-256 e1be9150da94db4a5c87c003c347a556c60912c6d24a9ac11a71f84837184396

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d01cbbfaccf928e6ce992171278c81d9a58dee1e99d53c0737157125f64989bf
MD5 b5e74f75559dadf486c45c66f54784fd
BLAKE2b-256 5d1a68d3c39ce86f39338fc19b659e05268a39a0fb7ffe23740bf6e3b44ac6f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c14e9d4631803f5e03988809dfa4a70d2a0985b2ae7260d114134771942ea8b4
MD5 ac7d669fc880628d0c6c4a1ebb900d77
BLAKE2b-256 b0579da8abcd798e0402feaaa2265a9743f0fc0bffc84e1b6197b547cb339497

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 9b4a7d38d5c2edf85aeffa7e80b24d4f1255bfb36efe317f2905d598f4b6f0bd
MD5 6944ca8a88ca4bc43088f8c3a23f3f70
BLAKE2b-256 7888efde9643349e3c384a1244814093874b8479ddd4984a1efa4a8fddbe1291

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4ac29185247314df6739f48ea5d8de2f5f84b5731d65d4f6ed5c1e55f8c2a98
MD5 4444bef0f0864339febd4543c0d93978
BLAKE2b-256 132b5ff0717bc1c08b71080c25443c4cbe2ca96a62a18370d266a10964065603

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