-
-
Notifications
You must be signed in to change notification settings - Fork 349
feat(mcp/client): add MCP client CLI for querying the code graph via MCP server #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,120 @@ | ||||||||||
| """MCP client for querying the code graph via the MCP server. | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| This module provides a simple CLI client that connects to the MCP server | ||||||||||
|
|
||||||||||
| and executes the ask_agent tool with a provided question. | ||||||||||
|
|
||||||||||
| """ | ||||||||||
|
|
||||||||||
| import asyncio | ||||||||||
| import json | ||||||||||
| import os | ||||||||||
| import sys | ||||||||||
| from typing import Any | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 15
Comment:
`Any` type is explicitly prohibited. Remove this import and use TypedDict for dict shapes.
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||
|
|
||||||||||
| import typer | ||||||||||
| from mcp import ClientSession | ||||||||||
| from mcp.client.stdio import StdioServerParameters, stdio_client | ||||||||||
|
|
||||||||||
| app = typer.Typer() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| async def query_mcp_server(question: str) -> dict[str, Any]: | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Then define: class MCPResponse(TypedDict):
output: strContext Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 24
Comment:
`dict[str, Any]` violates strict typing requirements. Use TypedDict instead:
```suggestion
async def query_mcp_server(question: str) -> MCPResponse:
```
Then define:
```python
class MCPResponse(TypedDict):
output: str
```
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
| """Query the MCP server with a question. | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| Args: | ||||||||||
|
|
||||||||||
| question: The question to ask about the codebase | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| Returns: | ||||||||||
|
|
||||||||||
| Dictionary with the response from the server | ||||||||||
|
|
||||||||||
|
Comment on lines
+25
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstrings are not allowed per the Comment Policy. Code should be self-documenting. Only comments with Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 25-38
Comment:
Docstrings are not allowed per the Comment Policy. Code should be self-documenting. Only comments with `(H)` prefix are allowed.
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||
| """ | ||||||||||
|
Comment on lines
+25
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
|
|
||||||||||
| # Start the MCP server as a subprocess with stderr redirected to /dev/null | ||||||||||
|
|
||||||||||
| # This suppresses all server logs while keeping stdout/stdin for MCP communication | ||||||||||
|
|
||||||||||
| with open(os.devnull, "w") as devnull: | ||||||||||
| server_params = StdioServerParameters( | ||||||||||
| command="python", | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding
Suggested change
|
||||||||||
| args=["-m", "codebase_rag.main", "mcp-server"], | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| async with stdio_client(server=server_params, errlog=devnull) as (read, write): | ||||||||||
| async with ClientSession(read, write) as session: | ||||||||||
| # Initialize the session | ||||||||||
|
|
||||||||||
| await session.initialize() | ||||||||||
|
|
||||||||||
| # Call the ask_agent tool | ||||||||||
|
|
||||||||||
| result = await session.call_tool("ask_agent", {"question": question}) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line passes the untrusted References
Comment on lines
+47
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded strings should be moved to a constants file. Create Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 47-59
Comment:
Hardcoded strings should be moved to a constants file. Create `codebase_rag/mcp/constants.py` for strings like `"python"`, `"-m"`, `"codebase_rag.main"`, `"mcp-server"`, `"ask_agent"`, `"question"`.
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
|
|
||||||||||
| # Extract the response text | ||||||||||
|
|
||||||||||
| if result.content: | ||||||||||
| response_text = result.content[0].text | ||||||||||
|
Comment on lines
+63
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No type narrowing before accessing
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 63-64
Comment:
No type narrowing before accessing `result.content[0]`. This will raise `IndexError` if content is empty. Add length check:
```suggestion
if result.content and len(result.content) > 0:
response_text = result.content[0].text
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||
|
|
||||||||||
| # Parse JSON response | ||||||||||
|
|
||||||||||
| try: | ||||||||||
| parsed = json.loads(response_text) | ||||||||||
|
|
||||||||||
| if isinstance(parsed, dict): | ||||||||||
| return parsed | ||||||||||
|
|
||||||||||
| return {"output": str(parsed)} | ||||||||||
|
|
||||||||||
| except json.JSONDecodeError: | ||||||||||
| return {"output": response_text} | ||||||||||
|
|
||||||||||
| return {"output": "No response from server"} | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @app.command() | ||||||||||
| def main( | ||||||||||
| question: str = typer.Option( | ||||||||||
| ..., "--ask-agent", "-a", help="Question to ask about the codebase" | ||||||||||
| ), | ||||||||||
|
Comment on lines
+84
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The PR description explicitly states that this client is "Useful for scripting and CI pipelines." In such environments, the Furthermore, unlike the primary CLI implementation in References
|
||||||||||
| ) -> None: | ||||||||||
| """Query the code graph via MCP server. | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| Example: | ||||||||||
|
|
||||||||||
| python -m codebase_rag.mcp.client --ask-agent "What functions call UserService.create_user?" | ||||||||||
|
|
||||||||||
|
Comment on lines
+88
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstrings are not allowed per the Comment Policy. Code should be self-documenting. Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 88-95
Comment:
Docstrings are not allowed per the Comment Policy. Code should be self-documenting.
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
| """ | ||||||||||
|
Comment on lines
+88
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
|
|
||||||||||
| try: | ||||||||||
| # Run the async query | ||||||||||
|
|
||||||||||
| result = asyncio.run(query_mcp_server(question)) | ||||||||||
|
|
||||||||||
| # Print only the output (clean for scripting) | ||||||||||
|
|
||||||||||
| if isinstance(result, dict) and "output" in result: | ||||||||||
| print(result["output"]) | ||||||||||
|
|
||||||||||
| else: | ||||||||||
| print(json.dumps(result)) | ||||||||||
|
|
||||||||||
| except Exception as e: | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catching bare Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 111
Comment:
Catching bare `Exception` is too broad. Catch specific exceptions like `ConnectionError`, `TimeoutError`, or create a custom exception type.
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||
| # Print error to stderr and exit with error code | ||||||||||
|
|
||||||||||
|
Comment on lines
+41
to
+113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline comments without Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 41-113
Comment:
Inline comments without `(H)` prefix violate the Comment Policy. Remove all comments or prefix them with `(H)` if they provide essential human context that cannot be expressed in code.
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||
| print(f"Error: {str(e)}", file=sys.stderr) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Suggested change
Context Used: Rule from Agentic Framework
Prompt To Fix With AIThis is a comment left during a code review.
Path: codebase_rag/mcp/client.py
Line: 114
Comment:
Use `loguru` for error logging instead of `print()` to stderr:
```suggestion
logger.error(f"Error: {str(e)}")
sys.exit(1)
```
**Context Used:** Rule from `dashboard` - ## Technical Requirements
### Agentic Framework
- **PydanticAI Only**: This project uses PydanticAI... ([source](https://app.greptile.com/review/custom-context?memory=d4240b05-b763-467a-a6bf-94f73e8b6859))
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
|
|
||||||||||
| sys.exit(1) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| if __name__ == "__main__": | ||||||||||
| app() | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the project's general rules, docstrings are not allowed. Please remove this module docstring.
References