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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.16-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.16-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.16-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 de53fedbd77d74e80f242b90aba6bf082d308dc07e737051460bd7a42919687d
MD5 ef41b1b03893006731c5bfc549f1adf1
BLAKE2b-256 6cde0db4e113cec9590a8065486d1cead6a955b8971a25d5bde870182106a417

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 63e6808fe0d81175a1b73e6dc7bd6258d2ed9d9d251b9530bd65daa285d42bfc
MD5 c664074d58bb22c9149672c41a53888c
BLAKE2b-256 dfb32a212afeb8b1b6b1f40d3116cc75ae62fb224090863d773cae34602d3a36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3ff85e3901b482d4fcd7bee19f0d271bab10b900d4c6823e843aaf2fb2b73bdf
MD5 dee6991c2459b3aa3492b40b6c809ff3
BLAKE2b-256 e6bb408b5696d21f39a52081bef85de7829def7a358840bed6049a7ad8ffacb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 061df8cba24dbc8c46722a2ff1b00c5194b9028a6c9ae43837cf0de7da3a47e8
MD5 6be89a4a27df801fdac9beddb514f0b8
BLAKE2b-256 fd7df6bb6163d98227738feb3ddf1b2e706a980016cf8fe3e761900d772bb46d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 490895f5465dc7366cbc0638f5a503ffbef82d0bb5fa81885471356dd6900712
MD5 19629cca0b5660b9c1fcfbb05cf521a6
BLAKE2b-256 7c55dd8c3241ce341401f3259bd69520c4d2be1b62c87eafb08f28312d2bad49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1bc0b3904aafcb7938b43a12210bf842efe15fb9e13644f31b8adcad057ea04e
MD5 85756f0c9e008f06a567270ba6a0ec0e
BLAKE2b-256 081506ddd447ffb320d6f1500c5a73d34753d5b8dd11078d040a26f0586cddd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 33b4f6402d8850342044dc082bfceff7724a351d40972748e5fa8b892682ee24
MD5 e09032766a4ef40985ad1159c23afebe
BLAKE2b-256 cb776e8ccbc57fb6c227b5252626b6cea7ad250653b7429b9f43ed79cca1b013

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 d4e90b65c9946391918623159c06a94af671b6a39c479272707e10976282774f
MD5 f078bdc6ef28a829ca9b2450685cdb89
BLAKE2b-256 a445cbe297f92caa5e57715e4afc52946ac709d0b9a699e8c3cc93cc5e72920b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bdcc0e5f2650e15bfbe9d0c58c3d312fdcd8dfc0cd3ecb57ff5fa8cf62b36f37
MD5 56af2c643b82b03fe4d7eecdc1b718ba
BLAKE2b-256 b923246c8aaaa3dd4219fe430944b362971ed548304bb65eb507ad981119cdc8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8c34c4551900d5bce5375cf7e813a2ed6fee79f897b426574a9e1808aa7c595b
MD5 4134a0432d3d729e616134fa16e0ca18
BLAKE2b-256 85b9f27b002dbdddbd0427bf1f4494684c91c1c7e27ac0182ebd25aa9738dd02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 4f6c9be1a294cfd1e17af459c195e857af5e1193155cab5ef9b321163bb9d2d9
MD5 7c7d7420897122128246e95da0ebe17b
BLAKE2b-256 c1d3a0adee04e5f58240e26dfc54e510a5dc4a584bb0dde0120cc99ec3f62bb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.16-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d8f879f086f6b800e255db8d47d4f95887194c50c300e98b01a89f6adee3e4bc
MD5 f0c4bef5b99debe0a5dd5e3ecaf8206a
BLAKE2b-256 36912b819af1669abf5b933f92d40ea758dc68683152c07eacccf45e17eb002c

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