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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b25cf1cebf345d05ce731fe479e01013325a63bff4e4404615ce31175d125337
MD5 143f28f1421b8e20b86c230a101f77c8
BLAKE2b-256 47fd5ddc6ccc58a6d6ecde877d139624d88dbb7a9ecf5d64c50937c58b522e9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 f8be82c26f8f551cdbc1642c7fd8fd83a2ddf14e3d74e0046b5622248afbd9c9
MD5 410219ec7b8f2ddd4ed6b169fcbbe545
BLAKE2b-256 fdc3f1d98a829dc8e145f59409b576ddd13ec1338d86e7c0f7dba46c8e006051

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b28ab0385e3b1ae17e8459253a8eb50fb765dfd37d9120739647791b2a50ed04
MD5 1fd516405d5563119bd7fb70b8efd081
BLAKE2b-256 2b3fc5f3d37f4e8f07f426c7aec258de3d23a304085bbbd964afe10259407344

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 73a62c86d70b2cd6388d5aa9577923a0358104a501a64f8da548a449162cf352
MD5 5436a5e0e2ae8f7020ef59e5c9e12741
BLAKE2b-256 229903b3087ab05e7dedc27e4c516bf18fb96ff9d6bb4e34220f62c998db5ac6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a1a318aae2e981d91327b5fcb06c75c8736fbc8f52e144e26dc9497aadb0229a
MD5 a365aa7a4ff6a35716a54eb8340c9900
BLAKE2b-256 7ece15ca6d511f2443a26fd74dec2e410d01410b5e98bbad2afb0162ece00132

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5c3a549169cf8f1e1facea425fadd89f26ffd6a0aa1f14ce7573aea3f36ac68b
MD5 735dde7ffb15a9be6943e5a692a901ae
BLAKE2b-256 939328eee7fd1a447f9d893f814d950208054a65d419822ac47f42544dd17069

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 91919aef23139521ba297de094fa4606b831be94a57caea0f165100ea0cbd975
MD5 cd13315d2657b2d124c3c2f681059fd8
BLAKE2b-256 153543eb8b64c3858e00d14c470551edb2161891355e68ddf32d7f38eed49c1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 193b09fc30338b3edf05024e98c512364cc07221763e831f01a7012e2331e706
MD5 15d3a0ccb30465b13052a79c2a9885ca
BLAKE2b-256 9109ae6404d011bf5597db57a3ece67426f693ebc86e954055f394178b6ef70e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b29cdce0ff0b85595bb6dbd0b09d7c67d1d4197cf5186f7ab8cd0f31d4eaf778
MD5 ec542f1e98bd7048403fe2f04ec6a4ab
BLAKE2b-256 449075dd667175e5a4d0ec26785246366c13f6c091bfb2f42f87a651443e889c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6a9f2081c25de073156e3c9e7f1a2f4db0a5660b8ee464327f627ac45b0e26d5
MD5 9d56669d7e07f02555779f5e671bd797
BLAKE2b-256 a3ceaa32f96d199c6abd4dab5ddcfd6f61c4a1d34adb0e12f859b5fa95f5ed33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c788121a1168628282a1c31d1ab3b377e6d1a8b89b28ac635c6550333b84c5a2
MD5 979957660bd9f4af2ff91fd52e1d9b6c
BLAKE2b-256 4debac8db492954e0e397f122741928bd7640c91f5f56bf97a3955135308d2e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.15-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e628b0f790cc8b5f8a2479cb065e203bb27de027f704000140138db31ecaeca9
MD5 a325d9f5afada61ef8196b3a14d45241
BLAKE2b-256 7c841fd5fec1f4ff3cb3030053e1fa0fa00f0ca896e69e4c196e4788a908158b

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