Skip to content

Refactor MCP client initialization and fix method resolution in _mcp.py#9

Closed
daatsuka wants to merge 5 commits intoqntx:mainfrom
daatsuka:fix/issue-7
Closed

Refactor MCP client initialization and fix method resolution in _mcp.py#9
daatsuka wants to merge 5 commits intoqntx:mainfrom
daatsuka:fix/issue-7

Conversation

@daatsuka
Copy link

@daatsuka daatsuka commented Mar 23, 2026

Summary

Implements MCP client support and a generic x402-fetch wrapper as requested in #7, adding both synchronous and asynchronous variants with full test coverage.


Changes

New files

src/x402_openai/_http.py (+109 lines)

  • create_x402_session(wallet, ...) — synchronous requests.Session subclass that intercepts HTTP 402 responses, signs the x402 payment challenge, and retries automatically
  • create_async_x402_session(wallet, ...) — async httpx.AsyncClient equivalent

src/x402_openai/_mcp.py (+331 lines)

  • X402MCPClient — synchronous MCP JSON-RPC 2.0 client with x402 payment handling; supports context-manager usage
  • AsyncX402MCPClient — async version using httpx.AsyncClient; supports async with
  • Both clients handle result: null / non-dict results with explicit ValueError (JSON-RPC 2.0 §5)

src/x402_openai/_cli.py (+60 lines)

  • create-x402-wallet entry-point: generates an EVM keypair, saves to ~/.x402/wallet.json with 0o600 permissions

Modified files

src/x402_openai/__init__.py (+20 / -1)

  • Re-exports X402MCPClient, AsyncX402MCPClient, create_x402_session, create_async_x402_session
  • Updated module docstring with usage examples for both HTTP and MCP clients

pyproject.toml (+3)

  • Registered create-x402-wallet = "x402_openai._cli:main" as an installed script

Tests

File Tests added
tests/test_http.py +164 lines — 8 test cases covering sync/async sessions, retry on 402, non-402 passthrough, signing failure
tests/test_mcp.py +353 lines — 18 test cases covering JSON-RPC dispatch, 402 retry, error handling, None/non-dict result guard
tests/test_transport.py +3 / -1 — minor fixture update

Local verification

$ pip install -e .
$ pytest tests/ -v
...
61 passed in 1.37s

All 61 tests pass (5 test modules: test_http, test_mcp, test_transport, test_wallet_factory, test_wallets).

Closes #7

daatsuka and others added 5 commits March 23, 2026 16:01
Feature: Add MCP client support + generic x402-fetch wrapper
Feature: Add MCP client support + generic x402-fetch wrapper
Feature: Add MCP client support + generic x402-fetch wrapper
@gitctrlx gitctrlx closed this Mar 23, 2026
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.

Feature: Add MCP client support + generic x402-fetch wrapper

2 participants