-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
188 lines (149 loc) · 6.17 KB
/
client.py
File metadata and controls
188 lines (149 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import asyncio
import os
import json
from typing import Optional, List, Dict, Any
from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from dotenv import load_dotenv
load_dotenv()
class NtropyMCPClient:
"""Client for testing the Ntropy MCP server"""
def __init__(self):
"""Initialize the client"""
self.session: Optional[ClientSession] = None
self.exit_stack = AsyncExitStack()
async def connect_to_server(self):
"""Connect to the Ntropy MCP server using uvx"""
api_key = os.environ.get("NTROPY_API_KEY")
# Create server parameters
server_params = StdioServerParameters(
command="uvx",
args=["ntropy-mcp", "--api-key", api_key],
env=None
)
# Set up the connection
stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
read, write = stdio_transport
self.session = await self.exit_stack.enter_async_context(ClientSession(read, write))
# Initialize the connection
await self.session.initialize()
# List available tools
response = await self.session.list_tools()
tools = response.tools
print("\nAvailable tools:")
for tool in tools:
print(f"- {tool.name}: {tool.description}")
return tools
async def call_tool(self, tool_name: str, **kwargs):
"""Call a tool on the server
Args:
tool_name: Name of the tool to call
**kwargs: Arguments to pass to the tool
Returns:
The result of the tool call
"""
if not self.session:
raise ValueError("Not connected to server. Call connect_to_server first.")
print(f"\nCalling tool: {tool_name}")
print(f"Arguments: {json.dumps(kwargs, indent=2)}")
result = await self.session.call_tool(tool_name, kwargs)
print("\nResult:")
print(json.dumps(result.content, indent=2))
return result.content
async def check_connection(self):
"""Check connection to the Ntropy API"""
return await self.call_tool("check_connection")
async def create_account_holder(self, id: str, type: str, name: str):
"""Create an account holder"""
return await self.call_tool("create_account_holder", id=id, type=type, name=name)
async def enrich_transaction(self, id: str, description: str, date: str,
amount: float, entry_type: str, currency: str,
account_holder_id: str, country: str = None):
"""Enrich a transaction"""
args = {
"id": id,
"description": description,
"date": date,
"amount": amount,
"entry_type": entry_type,
"currency": currency,
"account_holder_id": account_holder_id
}
if country:
args["country"] = country
return await self.call_tool("enrich_transaction", **args)
async def list_transactions(self, account_holder_id: str, limit: int = 10, offset: int = 0):
"""List transactions for an account holder"""
return await self.call_tool("list_transactions",
account_holder_id=account_holder_id,
limit=limit,
offset=offset)
async def bulk_enrich_transactions(self, transactions: List[Dict[str, Any]]):
"""Bulk enrich multiple transactions"""
return await self.call_tool("bulk_enrich_transactions", transactions=transactions)
async def cleanup(self):
"""Clean up resources"""
await self.exit_stack.aclose()
async def run():
"""Run a demonstration of the Ntropy MCP client"""
client = NtropyMCPClient()
try:
# Connect to the server using uvx
await client.connect_to_server()
# Check connection to Ntropy API
print("\n1. Checking connection to Ntropy API...")
await client.check_connection()
# Create an account holder
print("\n2. Creating account holder...")
account_holder = await client.create_account_holder(
id="test_user_123",
type="individual",
name="Test User"
)
account_holder_id = account_holder.get("id", "test_user_123")
# Enrich a single transaction
print("\n3. Enriching a single transaction...")
await client.enrich_transaction(
id="tx_001",
description="AMAZON.COM*MK1AB6TE1",
date="2023-05-15",
amount=-29.99,
entry_type="debit",
currency="USD",
account_holder_id=account_holder_id,
country="US"
)
# Enrich a batch of transactions
print("\n4. Bulk enriching multiple transactions...")
transactions = [
{
"id": "tx_002",
"description": "NETFLIX.COM",
"date": "2023-05-16",
"amount": -13.99,
"entry_type": "debit",
"currency": "USD",
"account_holder_id": account_holder_id
},
{
"id": "tx_003",
"description": "Starbucks Coffee",
"date": "2023-05-17",
"amount": -5.65,
"entry_type": "debit",
"currency": "USD",
"account_holder_id": account_holder_id
}
]
await client.bulk_enrich_transactions(transactions)
# List transactions for the account holder
print("\n5. Listing transactions for account holder...")
await client.list_transactions(account_holder_id)
except Exception as e:
print(f"\nError: {str(e)}")
finally:
# Clean up resources
await client.cleanup()
if __name__ == "__main__":
asyncio.run(run())