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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 26f36ef3c28384ae387600ce63f1cc989d31269a972b3afcdc19b5d80a9d5271
MD5 3de9142037170977007e86b49d323707
BLAKE2b-256 1d6403297beeaae2915f80fc77e76de47378526c5842b43698d5a48a7e644daa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 fac7784420fbc68be662a555041b924246791cfb4bb16cc77954f0fa15cfd5af
MD5 bf6df3a8bfd38daa1b2bf6ab627fbec8
BLAKE2b-256 f35c669b5e37b98a8231fe8fe6a0e0b49505fe46632a1664d4387666f073099c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1dd588e0808938cf211f4bb7d91814801423cbafd0519ff7d085c0498d33e031
MD5 0b00b6d8e031c40289de4853ee5f922a
BLAKE2b-256 767c20b18444ae1992e201b368a2b55626e38846b70ec618a60fb40f1f70c17d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 39d4e694e1468b7d83ebd703f37822f13c1a27d5c026c9ea19ee5805a62e5f9a
MD5 02565492481acb2a65cc2ae3e8f2f02e
BLAKE2b-256 a95ae89586b90b9cacfba9609b71c93198476f70e1ffd2c217fe4ce03c54af47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 456a9a2739a7dc82f3ff74614302eb56c116fb4363ec869f9c340557481e3a4a
MD5 e1df64308a760220c2b6803c364b7201
BLAKE2b-256 a37fb70755bfee70685018e286d69a74b9e008d256740620e6368035d5b60b41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a46ef03c6a1b662186d8a0495ad31e74899c53658d939a25922b7cf75325c483
MD5 35c78d014c8f5f665e88545d51486e8b
BLAKE2b-256 4b8c23524dd959e44b9fbc4aa0608ed627edf951a67421550d634f7011a201f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9717294a3d687688c2a22be72c2e4c428329dabef45af64c1d6899bb2eebf492
MD5 cccf4d97f11b0c0c956a8c01a508bd16
BLAKE2b-256 1808449e7f6b3720ff7ac99ea2d182c369fe9f7e96f9245c961ffda1ce1630cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 83ab1ad9e607af975e6920ddc84cad628521b2b852b2f80347514cc78cc02b24
MD5 96f71df263d3a4a62831edb7909d90ae
BLAKE2b-256 b5b6b70b77d6c255d84272b1c6b81b73550d1db4155aec3b5bab6116927fa3e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e5f5bc11d65a48b61c5ce2c4ae715f9b6e357cbce3636446958a974382f8572e
MD5 e8cb63df87ea84c1782556179a8dd3f4
BLAKE2b-256 2b7a9e194f593c7f0dffc9921a10b16f57296fa479d629797ca60bb30e464b15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 daafc2317270e7bb431428e0e133da4fc2721e496b661d9e0a12f99de1cb506f
MD5 1a68a1bde47fee6c809f7d278b2e9ae9
BLAKE2b-256 258a1372df13540deb1fc1b5377958857ba3c9d21d7726f08c8810ca2c2ba3e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 fed8340390e635554e220d82f63fa7c9fa67566ed0d694b7ac03d352e73665f8
MD5 ba8e77b6601d938051cfa234d92d36ba
BLAKE2b-256 5a7bbc8acaafacecbfd77b853ad10b3bdbda18cb119bdbcc9e40bac720d3d5b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.13-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0735006934bc98d1038d582ae4480aa16372dd4c636535b53aae868fa0c0b384
MD5 b8486f0de0860ea6d434807cb6f27b41
BLAKE2b-256 198f61007e739ec731bb8fbf6bbfbab9164cd76e5a9c8bb0a3381c58ec6c9083

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