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) 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.

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8d2f9b06ed9a33c10614be1607831a1c21a925fea54591bbfa1c07a80d72c267
MD5 3fad27b12ee5da14e1b2deb0c694186e
BLAKE2b-256 946756ce09f5449e9861f96b2c3f1407acf6eb72d2fb7e58d8c9150fa4a0a146

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d5f161b6ce17677fb46f0eda5cbf94a592f2955d71121ebc2ad98b2ef043dcba
MD5 a7bc8e8fb53f35808e79c96e247f92b5
BLAKE2b-256 34e68ee337475b9bdc97c0c0b123b38e5c812f52e02c76fbc19e78b5681b1ba4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dc36079c557e84ede3c36d2eaab550e7405b5b2f36bc10a459711b4275bd3608
MD5 85b25986e403e32169379239a1e584ec
BLAKE2b-256 7b220caf04a08c93fc1d3d39305b761ed3b11d57f6e34a214b031a0b51bd4a73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fdda6385873019f79fe9bcbecc2b31bd98bbcfd3843935915384be88438d1609
MD5 8f6cde81fc75b612c5a5a1d688397cb8
BLAKE2b-256 0b69be9b1c3e4615966a11447e88d1b7dd069035a4455429a0465c8c833f0061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 4dbd3e756ccca688b8a7ef41540cb51264a278e46c3c29ca3c35349157966216
MD5 67174bc615f8f7062d8c5634a35a00eb
BLAKE2b-256 97a6e5e23ff774ac104ee9a4a97b02bc3295541867b2bffc9eb9e098be97b373

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 49aac6c2869308388c4adb4dd6017f46ce48afc2c55405047b19951f2ee04c98
MD5 1fde1b976e3478e10352ae90ab63561b
BLAKE2b-256 7e9e31740cb5fcf0171e25e946c64e1d37157a6469c4489cbc8c45c735e006e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e2f2cbc0835ca413f0e2ed809f8a794b8f259f4cb526178023dbbcfa21b68c20
MD5 333b67e58408db86d1520d1220c609f1
BLAKE2b-256 f06429db845a1c491125d86bed79e757020c5b60ec6454643861bfb4ad338fee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 2ebbb4056fe3200db932de9eaefc62a541cf3cf09baa85de8db1d417e9d40c0e
MD5 a104adc51aa609b18edc79482dc0de57
BLAKE2b-256 71522999ee4bb39d41e405f872e4d5bba13073ccc4554a0ed98ae2fa724b8d66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6b02266a8140f77795259c3cef9b857b5845e6fc62f0aafdf3865f36d24d8029
MD5 f79c56fa8fb4fd342224620ba286e1c2
BLAKE2b-256 6a5c0094fb86694bcb125bc4cde6e1b4d7b06f25d606a80abd6ab9c0388d12af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 bc1d191c39e767ead07a2ef23c4a958f6e46f84722a60c55fa95d7883849c07c
MD5 11a323b8a212dba0c4562c9c7a30b0e8
BLAKE2b-256 d53136af3eb134cb9b957863eb72876c4fd87f546ec3d2e54f9eed7dc71d218d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 1b000a5e8ea4512766d9975829408c9e21f51f174347e5fd866a39994dfd6928
MD5 3c2fa8894a3935befca4ac56c380fb57
BLAKE2b-256 4703d24a04231ca3e5fcb806e915d58aacfca30eb083398d738ad2fd634967d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 23ea764ac27ddbd66fa70a6b3b99d4b9013153da1be1638372555db551152783
MD5 935a306ea2eb7ee5e8caae7698329303
BLAKE2b-256 6bbd5847f70787139bd3d844d85e819254d2f4359951333e7867da9f14a5921a

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