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
has_git_token Returns whether a usable GITHUB_TOKEN is reachable (used for honest tool listing)
ElementCache Drill-down cache for collapsed elements (code blocks, comments, patches, thread segments) in GitHub discussions
html_to_text Lightweight HTML → plain-text converter (markdown-flavoured)
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
mcp_methods.fastmcp Composable tool registrations for FastMCP servers — see below

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_issue("owner/repo", 123)

# Subsequent calls return cached summary (no network)
summary = cache.fetch_issue("owner/repo", 123)
# → "Cached owner/repo#123 — 5 elements available: cb_1, comment_2, patch_1, patch_2, patch_3"

# Force re-fetch when the issue has changed upstream
text = cache.fetch_issue("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"])

mcp_methods.fastmcp — drop-in tools for FastMCP servers

If you're running your own FastMCP server but want the same tool surface the bundled mcp-server binary ships (source navigation, graph overview, Cypher with CSV export, save_graph), import these helpers and register them on your app:

from mcp.server.fastmcp import FastMCP
from mcp_methods.fastmcp import (
    register_overview,
    register_cypher_query,
    register_source_tools,
    register_save_graph,
    serve_csv_via_http,
)

app = FastMCP("My Server")
register_overview(app, graph, overview_prefix="My custom guidance")
register_cypher_query(app, graph, csv_dir="temp/")
register_source_tools(app, source_roots=["./source"])
register_save_graph(app, graph)
_server, base_url = serve_csv_via_http("temp/")  # optional CORS-enabled HTTP server
app.run(transport="stdio")

Each helper is a thin (~10-line) wrapper over the existing Rust PyO3 surface — there's no logic duplication between the YAML-driven binary and these helpers, so agent behaviour is identical regardless of which path booted the server. graph is any object exposing describe() / cypher() / save(); kglite's KnowledgeGraph satisfies it. A runnable end-to-end stub lives at examples/fastmcp_demo.py.

Deployment — mcp-server binary

This crate also ships an MCP server binary at crates/mcp-server. Build and install it with:

cargo install --path crates/mcp-server
# → ~/.cargo/bin/mcp-server

For deployments that previously pinned a binary path elsewhere (e.g. /opt/miniconda3/envs/embeddings/bin/kglite-mcp-server), drop a symlink:

ln -s ~/.cargo/bin/mcp-server /opt/miniconda3/envs/embeddings/bin/mcp-server

The binary is domain-agnostic — source tools + GitHub access + a manifest-driven tool surface. Downstream binaries (e.g. kglite-mcp-server) re-export mcp_server::McpServer::new(...) to layer graph-specific tools on top while reusing the boot sequence, .env loading, workspace mode, watch mode, and embedder lifecycle.

Cargo features

By default the framework includes Python extension support — YAML- declared python: tool hooks and the embedder: factory load via PyO3. Downstream binaries that want a framework without the crates/mcp-server-level PyO3 dep can opt out:

mcp-server = { git = "...", default-features = false }

Note: mcp-methods (the top-level crate) still pulls in PyO3 as a hard dep because the Python wheel surface depends on it. The feature gate above removes the direct dep from crates/mcp-server only — useful for keeping the framework's non-Python build path clean and for downstream binaries that don't want manifest-declared python: tools / embedder. For a fully PyO3-free build path, the consuming binary also needs to gate the mcp-methods dep itself.

Operating modes (set via CLI flag or the YAML manifest)

Mode How to set When to use
bare no flag testing the protocol layer in isolation
source-root --source-root DIR or YAML source_root: fixed local directory; no clone
workspace (github) --workspace DIR clone-and-track GitHub repos
workspace (local) YAML workspace: { kind: local, root: ..., watch: ... } fixed local dir + optional file watcher; alternative to the legacy code_review server
watch --watch DIR rebuild downstream artifacts on file changes

YAML manifest declarations win over CLI flags when both are set (same precedence rule as source_root:).

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

mcp_methods-0.3.24-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.24-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.24-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e1db634a3a7421116cd0c5c593c707cf080a6bd5fba0833cb716d133fc1aafbf
MD5 f851f24704e817044ca49952a954bb25
BLAKE2b-256 f0bec6f0e79207ea48a9ad005a82141f12011cb465f95d7fcfe68c2044b4dbf3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b83772d649d11c7869a43d893c96a3426df432cfb6b24e41e880e1e8780b9a41
MD5 db1bca3b3fa08630b5ff977f28e3c9b5
BLAKE2b-256 c6c3a7fde876d45c7ccb6ef7b7fed535aad2a728ede8a4caede4001a8da1984c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0ab44df53cf1c362628a0a49e3e532cc3bf6fc516b8b06b4cc5b8c9be33168bd
MD5 b51d35318c13d1a9c84cf222d0fbf948
BLAKE2b-256 02979f9bc39f375d840d98c20e568cdfb8bb72d176acd2c9c09452582cde7f52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6f445a594bf3a5aa8020f6da38c266d168d4a84333aebb727229f879f1b94245
MD5 ea388ac2a1ad6c30567ac3de88da35c4
BLAKE2b-256 854d18010b8b43ce5c46631b991d8447571ec8924cb7469cce3c0455f76c9c19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 61833006110c6b9a6fd4a13bc55579a618d0f897f17ba6f81c0554ebc936f73d
MD5 d55b7405476cfab643b0de53f983c80e
BLAKE2b-256 515997c302f9f43c4466ad8f309c9bac82f309bc3fb552a6a2ec3a4d3438e770

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f3130dbd4e6964f944614ef44f251da7ba5752764121ac61b831e5daac67aa33
MD5 5405588b65ecf86dcf3a03abbbafc9a1
BLAKE2b-256 e6bec48a29eed5b1244723a42654537023b7cc99a0ca52dbb868548b85a42583

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a9d2d78510d3d486d6e0b22df43c3db53413ed6aa6bae97d35828aca9426ee9a
MD5 0de74edbb11bb092faad0f3863ee19f3
BLAKE2b-256 f64417ab185d7ef4cb7f48fdced4930c7f1eac122fe00e4f4e07491a1ef0e7e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 6980ccd8366289c1c11aa41de6dad8ba06934266015de7170bc86e871b68323c
MD5 6dad70be7f59442bb3a4337ad1b40d4d
BLAKE2b-256 86578267b015532a4e3c1a9907b81d7e4be71585bd8cc5beac2e13e67d8d12e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 22834dda530ce9cc52b2424ce870d199f1ca1679b5d24677d02575b72dd1abd3
MD5 1c4e64ce4ec561acd9386375c6c0463c
BLAKE2b-256 c8c5481fd90c0ee2d631c7435126e39ef14371442c1a41adf3d700ef586a37d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 49a011337be017b5bc876d1cbcc66911df7fcf41e59f8200b479ca9912e78a75
MD5 0beea105374876dd26f56d7e0f968570
BLAKE2b-256 23337eb3ad545b964be77a0b289c5e84adb38dccb808e6c4300010a90c6d5f82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b3fc2e9d2425359e2ba1268e6fab9c114756810ffa7900f73b52122a93fb72d1
MD5 ca4394ef5f5e079f0549aabbfa398b70
BLAKE2b-256 6f23da406dc3f4f11aa713b87de25b614b8068e94a3b3922fe2321dc9738d5d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_methods-0.3.24-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c3c4369a57adbe44087b386ccc237e8897c1d4391a8395e60d3ea18acc36b50c
MD5 632c70ab2ea91c1b236300c281d8c49e
BLAKE2b-256 217f03e17b36b6c99c20e5a43685fa214f3d1c46074bc242e9eb48d6332d38b5

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