Add Context7 MCP documentation tool#23
Conversation
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
📝 WalkthroughWalkthroughThe PR updates the tool package initialization to enable side-effect imports for tool registration, then adds a new ChangesTool Registration and Context7 Documentation
🎯 2 (Simple) | ⏱️ ~8 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
cloudbot/agent/tools/__init__.pycloudbot/agent/tools/context7.py
|
|
||
| import requests | ||
|
|
||
| from cloudbot.agent.common import run_in_executor, safe_tool |
There was a problem hiding this comment.
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.
| 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.
| 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) |
There was a problem hiding this comment.
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.
Summary
Adds a Context7 integration as a new agent tool (
context7) that fetches real-time library/framework documentation from context7.com.What this adds
cloudbot/agent/tools/context7.pycontext7— accepts a library name or topic query, returns structured markdown docs with code examplescloudbot/agent/tools/__init__.pyHow it works
.context7 "react hooks"(or any lib/topic)https://api.context7.com/docs?query=...Example usage
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)