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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7cfb0ce76a90e7ac9624d0a22fd5ec920a32d48863dac3b1ce964c8fe6d14a25
MD5 c69673bddca6003378a1df0ddfa1bd61
BLAKE2b-256 e7873c41fe90a76ebafac2143e693020f1bc2d25878c7e7e62185a787749f31c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b712aec1a476ed59747cf418029ea1bcf3456a5e0325bbca0ae0a00a7451decf
MD5 e117703bac98905dad54771cebe9df8f
BLAKE2b-256 8580e9908a8d053a1fdda88418e5da6959d3f73255111bb0efe806505409c1fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8817f988b37edf93aae654f4215d75842ce7796a5cd14e3690a07baae0ab3d74
MD5 20d415f9e71ee139569ddfbe33efbcab
BLAKE2b-256 6e259091ce1ae9e7272bccede46ba1e715f2bf6badad2b032d2bcbd3fc802476

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6c24d07332a88048a0f75c932c673fb0490b8869560b81ac954d88f28bdd7221
MD5 9e430b693d12e0e289c894071cef0012
BLAKE2b-256 02884e09aef62f069bba9f8703efde3753790f886338f38e1e178c66cac78a89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 2dd0bb26af24a7b0f23ffc4edc00e454d8186a9fdf23e2394063c0352dd95803
MD5 a7f0db0edfc76ea805b0215676b20fa8
BLAKE2b-256 af12f3c839dff74d7d3621192640af2bdaec8425945ef8248763d54874da090b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4c46ad3893eb47b3588166830ffc6063e5e1f410997de762b7442f2b647134f9
MD5 2f28133273d364e1aab1a7f64171cd30
BLAKE2b-256 d6dcd2deb4682a6aec9010826a5529e0bc82840e43ceb68148500b4d93402113

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 78fac40cd715c927d1c7dddb26b77f7e5876bf2b500ed3bb0a228495ae67f35b
MD5 dce326588b9e02afb4490f53fc727604
BLAKE2b-256 7eaa938c7acf03dd1bb88bac0abe1cd4207ab92a4e68e2e52c3424fbfe49cb24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 ff7b5694dfac4178a7f5b1956b3337c6cc3700d55a7fe0dfe34612d565a6f75a
MD5 b67ccbc5333e5041accc7d98f7ea9348
BLAKE2b-256 938454b47544977231197415aad6fd92b6de017c57a18854cfcdd6aa104a0bbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5fe6143c777d538388de4a945aee5b8d3ec716883b437c672eb9ec6225459d34
MD5 2f90ae0cf6f5f4821e36bb2d8b823534
BLAKE2b-256 d808e983b68ee4476ebc5c9c153d08621df2ca0263f69375591af171e4bdd063

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 67898331de6bcee68bdd97a6583dcb8b90c92bc4a35f40ff951c8bb78c372194
MD5 5232393ae723ee3723468bdc10a28c20
BLAKE2b-256 3e88b8751654ce42218011144096028cfbaefe3bc1fd04a293d8154b86e511e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 9d25f8578c6b1697cd9c4d362f1eaba7cdc678f235b9e9641017458666ef6ee8
MD5 6f880f023dd3fdce555655ec6a423033
BLAKE2b-256 bfae7506104acb90534aad83d1681000f4259dda7a7f4e216e8ea2cd055395e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 78578f0fe7297183548ee05490ddbeb7c346a6e44e092a859502f68f7de691fc
MD5 797b49f66b2fcb59d4eaa5130bada364
BLAKE2b-256 69b5c0a5b0d58ea0a034782c7eb1122969c73766158b2d55559354d15c3b1a70

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