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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b748bc921f507edfe12912c8eb0eb131e4019478605fdaeefea122f1ffdf36fa
MD5 87931eab0f4400fa9b252177c4db51cd
BLAKE2b-256 59e3b43ec30f1e122fb0ff2cf2293054daad4db58a384e9bad227310db6da250

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 5f00bc68c9736df97f64d23b732cba5d5e0219249abf1c8c999bde67176f36c2
MD5 203fee9f721b65516dcc697f0b75a899
BLAKE2b-256 a706e8ce47939c71cd3d60a97b88cf0fd25239d2f9914ccae35476a8c3339b7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9ac9aff07ced10d09b1a8838118e12c1b77af6ee40e9e209e60f6897c119c845
MD5 25367e76a0e719517da949c147b892fc
BLAKE2b-256 66281cd66efdb96a01877469d6193c34368a9d31fac58b2025d875538097ccd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e56855b851bf93787fb75e98c1172fce57767502b63a49d9104915388885e483
MD5 0cba7d3b9866b4dd750a88c95b9d41f9
BLAKE2b-256 274ae671a967b7e4dd15489d709b90c28359713105ae0f9d7f12d58d4d8d29b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3788bca40e923bdf95a989519bf9bfdeabb2655c2c9d9053814eda5a524208ef
MD5 086bc2fdd56ee4386d5c4c8512d6eaea
BLAKE2b-256 d1547ae961611febbdfeb182e96ae02dba70b2e9f8379d91c8f0add898da94fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 89ac1c0e73539f35198d7d2c92324057aece5303aed77c7c99ff5655b22b84f4
MD5 13266e0f9bd71b714b61dce4066647e7
BLAKE2b-256 c2035de0e24e5286e03b328e6591462b05d6497f65ad4398eeced821def867cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 fa31cd0975a46e5eba38a636744dc098c3e4284c29c05493ad60ff7f67e1c722
MD5 6a2d02e547fb5c2d040d59149c9b2442
BLAKE2b-256 4bcbeae72b35839b056095c47b5bcabeb16e5b13b81caac4ee170161df9af025

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 0303ca25a49c5cc0937950b94000894d23c91926b93e73b22dd6e3aaa25570f2
MD5 5ca0b67a346e8cddf83192b3ca1c7c3a
BLAKE2b-256 03ec2927bf2022f79b00c76261d48eeecfe3ce74a9e3d8935ed6842992da4359

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 72c6471e17efc935ca2c9cc5ef3c83a2688fb7fb6d07f46732c519d75e6352f6
MD5 62cb871674255070d0daee1647772f6d
BLAKE2b-256 c5c2fb06c13158487693920bd39d70a55d283d9f2d80e73b5f1675b2e96fd50e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c227abd6912ce402d6a2c6ea0d1b7c44e33910f291539a0595aefb1055825334
MD5 53e4933fcfa3aebac030d60b712ec9a6
BLAKE2b-256 cb810133217e1475ba1516ec3d88ec0af91efaf5c4ed767b507eba2d0cc0f449

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 0d0f1ad5c5a1d400afd4450926c73265786d9954428d849a195615fe2234d244
MD5 6a8f0a98f0e77db5d135a411f678c89e
BLAKE2b-256 f101d55c255ebe9d1e6ff3110fed68089a468ad588b7e8357faed7b3e9c7a0fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.12-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2d3a54e6884e549bc50b9a3b14a568ead710cceb2a3c61be56936ee4d52da643
MD5 c4a2fb9d1bf0760901db4a9c55159dcd
BLAKE2b-256 a5a0c059cc945a3243f4817342e16433d4e743337cb95527edbaef6b84ff09a8

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