Skip to content

Add Context7 MCP documentation tool#23

Open
h4ksclaw wants to merge 2 commits into
h4ks-com:mainfrom
h4ksclaw:feat/context7-mcp
Open

Add Context7 MCP documentation tool#23
h4ksclaw wants to merge 2 commits into
h4ks-com:mainfrom
h4ksclaw:feat/context7-mcp

Conversation

@h4ksclaw
Copy link
Copy Markdown

Summary

Adds a Context7 integration as a new agent tool (context7) that fetches real-time library/framework documentation from context7.com.

What this adds

  • New file: cloudbot/agent/tools/context7.py
  • New tool: context7 — accepts a library name or topic query, returns structured markdown docs with code examples
  • Registered in cloudbot/agent/tools/__init__.py

How it works

  1. Agent calls .context7 "react hooks" (or any lib/topic)
  2. Tool hits https://api.context7.com/docs?query=...
  3. Returns up to 5 doc entries, capped at 4000 chars total
  4. Error-wrapped so API failures don't crash the agent run

Example usage

.context7 fastapi dependency injection
.context7 next.js app router
.context7 tailwind css utilities

Why Context7?

Context7 provides always-up-to-date documentation for hundreds of libraries and frameworks — better than the agent guessing from training data or fetching random URLs. Gives the agent a dedicated "look up current docs" capability.

Files changed

  • cloudbot/agent/tools/context7.py (new)
  • cloudbot/agent/tools/__init__.py (1 line added)

h4ksclaw added 2 commits May 19, 2026 19:47
Integrates context7.com API as a new agent tool (`context7`) that fetches
real-time library/framework documentation. The agent can now look up current
API docs for any programming library by name or topic.

- New file: cloudbot/agent/tools/context7.py
- Tool registered via @tool decorator in the agent registry
- Returns structured markdown with code examples
- Capped at 5 entries / 4000 chars to respect token budgets
- Error-wrapped for resilience against API failures

Resolves: add Context7 MCP integration for better library doc access
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 19, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates the tool package initialization to enable side-effect imports for tool registration, then adds a new context7 tool that fetches and formats library documentation from the Context7 API based on user queries, with error handling and response formatting for both list and dict JSON payloads.

Changes

Tool Registration and Context7 Documentation

Layer / File(s) Summary
Tool registration infrastructure
cloudbot/agent/tools/__init__.py
Package initializer now imports tool submodules as side effects to register @tool decorators, with a docstring documenting that tool registration occurs on import.
Context7 documentation tool
cloudbot/agent/tools/context7.py
Backend _fetch_context7_docs() calls the Context7 API, handles request errors, and formats list/dict JSON responses as markdown with entry caps and content truncation; async context7 tool wrapper validates the query input and executes the fetch in a background executor.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 A new doc tool hops into the fold,
Context7 API stories to be told,
Registration springs alive with imports in sight,
Fetching frameworks' wisdom, shining so bright! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add Context7 MCP documentation tool' accurately and concisely summarizes the main change: introducing a new Context7 documentation tool.
Description check ✅ Passed The description clearly explains the Context7 tool integration, its purpose, implementation details, and usage examples, all directly related to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch feat/context7-mcp

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cloudbot/agent/tools/context7.py`:
- Line 15: Remove the unused import safe_tool from the import statement in
context7.py to fix the flake8 F401 violation; keep the required run_in_executor
import from cloudbot.agent.common and update the line that currently reads "from
cloudbot.agent.common import run_in_executor, safe_tool" to only import
run_in_executor so the module no longer references the unused symbol safe_tool.
- Around line 48-60: The joined output from parts is not being capped to 4000
characters; update the return logic so after creating final =
"\n\n---\n\n".join(parts) you enforce a 4000-character limit (e.g., if
len(final) > 4000 then truncate final to final[:4000] and append a clear "...
(truncated)" suffix) before returning; reference the parts list and the existing
join expression and perform the truncation right before the return to guarantee
the aggregated response cannot exceed 4000 chars.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 21b6a4cc-445a-4e96-9433-a995c34cb581

📥 Commits

Reviewing files that changed from the base of the PR and between 028f8ea and 29f3a73.

📒 Files selected for processing (2)
  • cloudbot/agent/tools/__init__.py
  • cloudbot/agent/tools/context7.py


import requests

from cloudbot.agent.common import run_in_executor, safe_tool
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Remove the unused safe_tool import (Line 15).

This is currently a flake8 F401 violation and can block CI.

Suggested fix
-from cloudbot.agent.common import run_in_executor, safe_tool
+from cloudbot.agent.common import run_in_executor
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from cloudbot.agent.common import run_in_executor, safe_tool
from cloudbot.agent.common import run_in_executor
🧰 Tools
🪛 Flake8 (7.3.0)

[error] 15-15: 'cloudbot.agent.common.safe_tool' imported but unused

(F401)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cloudbot/agent/tools/context7.py` at line 15, Remove the unused import
safe_tool from the import statement in context7.py to fix the flake8 F401
violation; keep the required run_in_executor import from cloudbot.agent.common
and update the line that currently reads "from cloudbot.agent.common import
run_in_executor, safe_tool" to only import run_in_executor so the module no
longer references the unused symbol safe_tool.

Comment on lines +48 to +60
for entry in data[:5]: # cap at 5 entries to stay within limits
title = entry.get("title", "Untitled")
content = entry.get("content", "") or entry.get("text", "") or ""
source = entry.get("source", "") or entry.get("url", "")
block = f"## {title}\n"
if source:
block += f"*Source: {source}*\n\n"
# Truncate long entries
if len(content) > 3000:
content = content[:3000] + "\n... (truncated)"
block += content
parts.append(block)
return "\n\n---\n\n".join(parts)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Enforce the 4000-character cap on aggregated list output.

Line 60 returns joined sections without a final cap, so multi-result responses can exceed the stated limit.

Suggested fix
 logger = logging.getLogger("cloudbot")
 
 _CONTEXT7_BASE = "https://api.context7.com"
+_MAX_RESPONSE_CHARS = 4000
+_TRUNCATION_SUFFIX = "\n... (truncated)"
@@
-        return "\n\n---\n\n".join(parts)
+        result = "\n\n---\n\n".join(parts)
+        if len(result) > _MAX_RESPONSE_CHARS:
+            cutoff = _MAX_RESPONSE_CHARS - len(_TRUNCATION_SUFFIX)
+            result = result[:cutoff] + _TRUNCATION_SUFFIX
+        return result
@@
-        if len(content) > 4000:
-            content = content[:4000] + "\n... (truncated)"
+        if len(content) > _MAX_RESPONSE_CHARS:
+            cutoff = _MAX_RESPONSE_CHARS - len(_TRUNCATION_SUFFIX)
+            content = content[:cutoff] + _TRUNCATION_SUFFIX
         return content
     else:
-        return str(data)[:4000]
+        return str(data)[:_MAX_RESPONSE_CHARS]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cloudbot/agent/tools/context7.py` around lines 48 - 60, The joined output
from parts is not being capped to 4000 characters; update the return logic so
after creating final = "\n\n---\n\n".join(parts) you enforce a 4000-character
limit (e.g., if len(final) > 4000 then truncate final to final[:4000] and append
a clear "... (truncated)" suffix) before returning; reference the parts list and
the existing join expression and perform the truncation right before the return
to guarantee the aggregated response cannot exceed 4000 chars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant