Skip to content

[Phase 1.2] Python MCP Client Wrapper - Type Safety Improvements#560

Open
anchapin wants to merge 1 commit intomainfrom
feature/issue-517
Open

[Phase 1.2] Python MCP Client Wrapper - Type Safety Improvements#560
anchapin wants to merge 1 commit intomainfrom
feature/issue-517

Conversation

@anchapin
Copy link
Copy Markdown
Owner

Summary

Implements type safety improvements to the Python MCP client wrapper to meet mypy --strict compliance.

Changes

  • Add explicit type annotation for JSON-RPC response parsing to satisfy mypy --strict
  • Add runtime type check for tool call results to ensure proper type safety
  • Fix __exit__ return type from bool to None (PEP 343 compliance)

Testing

All 62 existing tests pass with 94% coverage (exceeds 75% target).

cd agent && .venv/bin/python -m pytest tests/test_mcp_client.py -v
# ============================== 62 passed in 1.38s ==============================

Code quality checks pass:

  • ✅ mypy --strict: Success
  • ✅ black: Compliant
  • ✅ pylint: 9.23/10

Related Issue

Closes #517

Acceptance Criteria

✅ Create agent/mcp_client.py - COMPLETE
✅ Implement list_tools() method - COMPLETE
✅ Implement call_tool() method - COMPLETE
✅ Implement connection lifecycle (spawn, initialize, shutdown) - COMPLETE
✅ Add context manager support - COMPLETE
✅ Write tests using Hypothesis - COMPLETE
✅ Target 75% test coverage - EXCEEDED (94%)

Implementation Details

The Python MCP client wrapper provides a robust, secure interface to the MCP protocol:

  • Security: Command validation with allowlist and metacharacter detection
  • Reliability: 94% test coverage with comprehensive error handling
  • Usability: Context manager support for automatic cleanup
  • Type Safety: Full mypy --strict compliance

Example usage:

with McpClient("filesystem", ["npx", "-y", "@server/fs"]) as client:
    tools = client.list_tools()
    result = client.call_tool("read_file", {"path": "test.txt"})

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

- Add explicit type annotation for JSON-RPC response parsing
- Add runtime type check for tool call results
- Fix __exit__ return type from bool to None (PEP 343)

These changes improve type safety and fix mypy --strict compliance.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Sorry @anchapin, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

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.

[Phase 1.2] Implement Python MCP client wrapper

1 participant