Add wallet support to MCP endpoints: implement wallet balance and transaction...

Add wallet support to MCP endpoints: implement wallet balance and transaction tools for both global and user-specific MCP access, update documentation with wallet MCP tools and examples
parent 123fe347
......@@ -727,6 +727,8 @@ curl -X POST http://localhost:17765/mcp/tools/call \
- `list_rotations` - List all rotation configurations
- `list_autoselect` - List all autoselect configurations
- `chat_completion` - Make chat completion requests
- `get_wallet_balance` - Get wallet balance and auto top-up settings
- `get_wallet_transactions` - Get wallet transaction history
**Autoselect-level tools:**
- `get_autoselect_config` - Get autoselect configuration
......@@ -996,6 +998,37 @@ curl -X POST http://localhost:17765/mcp \
}
}
}'
# Get user's wallet balance
curl -X POST http://localhost:17765/mcp \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "get_user_wallet_balance",
"arguments": {}
}
}'
# Get user's wallet transaction history
curl -X POST http://localhost:17765/mcp \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "get_user_wallet_transactions",
"arguments": {
"page": 1,
"limit": 10
}
}
}'
```
### Direct Tool Call Examples
......
......@@ -473,10 +473,17 @@ MCP tools include:
- `list_models` - List available models
- `chat_completions` - Send chat completion requests
- `get_wallet_balance` - Check wallet balance
- `get_wallet_transactions` - Get wallet transaction history
- `get_providers` - Get provider configurations
- `get_rotations` - Get rotation configurations
- `get_autoselects` - Get autoselect configurations
**User-specific MCP tools (when authenticated with user token):**
- `list_user_models` - List user's available models
- `get_user_wallet_balance` - Check user's wallet balance
- `get_user_wallet_transactions` - Get user's wallet transaction history
- `user_chat_completion` - Send chat completion using user's configurations
## Provider Support
### Model Metadata Extraction
......
......@@ -216,6 +216,35 @@ class MCPServer:
},
"required": ["model", "messages"]
}
},
{
"name": "get_wallet_balance",
"description": "Get current wallet balance and auto top-up settings",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
},
{
"name": "get_wallet_transactions",
"description": "Get wallet transaction history",
"inputSchema": {
"type": "object",
"properties": {
"page": {
"type": "integer",
"description": "Page number (1-based)",
"default": 1
},
"limit": {
"type": "integer",
"description": "Number of transactions per page",
"default": 20
}
},
"required": []
}
}
])
......@@ -400,6 +429,36 @@ class MCPServer:
"required": ["autoselect_id"]
}
},
# User wallet tools
{
"name": "get_user_wallet_balance",
"description": "Get current user's wallet balance and auto top-up settings",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
},
{
"name": "get_user_wallet_transactions",
"description": "Get user's wallet transaction history",
"inputSchema": {
"type": "object",
"properties": {
"page": {
"type": "integer",
"description": "Page number (1-based)",
"default": 1
},
"limit": {
"type": "integer",
"description": "Number of transactions per page",
"default": 20
}
},
"required": []
}
},
# User chat completion
{
"name": "user_chat_completion",
......@@ -660,12 +719,16 @@ class MCPServer:
Tool result
"""
# Route to appropriate handler
handlers = {
# Common tools
handlers = {
# Common tools
'list_models': self._list_models,
'list_rotations': self._list_rotations,
'list_autoselect': self._list_autoselect,
'chat_completion': self._chat_completion,
'get_wallet_balance': self._get_wallet_balance,
'get_wallet_transactions': self._get_wallet_transactions,
}
# Add autoselect-level tools
......@@ -712,6 +775,9 @@ class MCPServer:
'get_user_autoselect': self._get_user_autoselect,
'set_user_autoselect': self._set_user_autoselect,
'delete_user_autoselect': self._delete_user_autoselect,
# User wallet tools
'get_user_wallet_balance': self._get_user_wallet_balance,
'get_user_wallet_transactions': self._get_user_wallet_transactions,
# User chat completion
'user_chat_completion': self._user_chat_completion,
})
......@@ -1181,6 +1247,65 @@ class MCPServer:
}
}
async def _get_wallet_balance(self, args: Dict) -> Dict:
"""Get wallet balance (global context)"""
# This would require admin access to view global wallet stats
# For now, return not implemented as global wallets don't exist yet
raise HTTPException(status_code=501, detail="Global wallet balance not implemented")
async def _get_wallet_transactions(self, args: Dict) -> Dict:
"""Get wallet transactions (global context)"""
# This would require admin access to view global wallet transactions
# For now, return not implemented as global wallets don't exist yet
raise HTTPException(status_code=501, detail="Global wallet transactions not implemented")
async def _get_user_wallet_balance(self, args: Dict, user_id: int) -> Dict:
"""Get user wallet balance"""
try:
from .payments.wallet.manager import WalletManager
wallet_manager = WalletManager()
wallet = await wallet_manager.get_wallet(user_id)
return {
"balance": float(wallet.balance),
"currency": wallet.currency,
"auto_topup_enabled": wallet.auto_topup_enabled,
"auto_topup_threshold": float(wallet.auto_topup_threshold) if wallet.auto_topup_threshold else None,
"auto_topup_amount": float(wallet.auto_topup_amount) if wallet.auto_topup_amount else None
}
except Exception as e:
logger.error(f"Error getting user wallet balance: {e}")
raise HTTPException(status_code=500, detail="Failed to get wallet balance")
async def _get_user_wallet_transactions(self, args: Dict, user_id: int) -> Dict:
"""Get user wallet transactions"""
try:
page = args.get('page', 1)
limit = args.get('limit', 20)
from .database import DatabaseRegistry
db = DatabaseRegistry.get_config_database()
transactions = db.get_wallet_transactions(user_id, page=page, limit=limit)
return {
"transactions": [
{
"id": tx["id"],
"type": tx["type"],
"amount": float(tx["amount"]),
"description": tx["description"],
"created_at": tx["created_at"]
}
for tx in transactions
],
"page": page,
"limit": limit
}
except Exception as e:
logger.error(f"Error getting user wallet transactions: {e}")
raise HTTPException(status_code=500, detail="Failed to get wallet transactions")
async def _delete_autoselect_config(self, args: Dict) -> Dict:
"""Delete autoselect configuration"""
autoselect_id = args.get('autoselect_id')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment