-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_example.py
More file actions
381 lines (318 loc) · 10.5 KB
/
Copy pathclient_example.py
File metadata and controls
381 lines (318 loc) · 10.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""Example client for Company Intelligence MCP Server.
This script demonstrates how to call the MCP server using FastMCP Client.
Usage:
# Make sure the server is running first:
# cd examples/mcp_server && uv run python server.py
# Then run this client:
# cd examples/mcp_server && uv run uv run python client_example.py "トレジャーデータ"
"""
import argparse
import asyncio
import json
import sys
from typing import Any
import httpx
from fastmcp import Client
async def call_tool_async(
server_url: str,
tool_name: str,
arguments: dict[str, Any],
) -> Any:
"""Call an MCP tool via FastMCP Client.
Args:
server_url: URL of the MCP server
tool_name: Name of the tool to call
arguments: Tool arguments
Returns:
Tool response
"""
# FastMCP client connects to the /mcp path
mcp_url = f"{server_url}/mcp"
client = Client(mcp_url)
async with client:
result = await client.call_tool(tool_name, arguments)
return result
def call_tool(server_url: str, tool_name: str, arguments: dict[str, Any]) -> Any:
"""Synchronous wrapper for call_tool_async."""
return asyncio.run(call_tool_async(server_url, tool_name, arguments))
async def list_tools_async(server_url: str) -> list[dict[str, Any]]:
"""List available MCP tools.
Args:
server_url: URL of the MCP server
Returns:
List of tool definitions
"""
mcp_url = f"{server_url}/mcp"
client = Client(mcp_url)
async with client:
tools = await client.list_tools()
return [
{
"name": tool.name,
"description": tool.description,
}
for tool in tools
]
def list_tools(server_url: str) -> list[dict[str, Any]]:
"""Synchronous wrapper for list_tools_async."""
return asyncio.run(list_tools_async(server_url))
def health_check(base_url: str) -> dict[str, Any]:
"""Check server health.
Args:
base_url: Base URL of the MCP server
Returns:
Health status
"""
url = f"{base_url}/health"
with httpx.Client() as client:
response = client.get(url)
response.raise_for_status()
return response.json()
def generate_report(server_url: str, company_name: str, enable_tracing: bool = True) -> Any:
"""Generate company intelligence report.
Args:
server_url: URL of the MCP server
company_name: Company to research
enable_tracing: Whether to enable Langfuse tracing
Returns:
Generated report
"""
return call_tool(
server_url=server_url,
tool_name="generate_company_intelligence",
arguments={
"company_name": company_name,
"enable_tracing": enable_tracing,
},
)
def search_news(server_url: str, company_name: str, max_results: int = 10) -> Any:
"""Search for company news.
Args:
server_url: URL of the MCP server
company_name: Company to search
max_results: Maximum results
Returns:
Search results
"""
return call_tool(
server_url=server_url,
tool_name="search_company_news",
arguments={
"company_name": company_name,
"max_results": max_results,
},
)
def search_trends(
server_url: str,
company_name: str,
industry: str | None = None,
max_results: int = 10,
) -> Any:
"""Search for industry trends.
Args:
server_url: URL of the MCP server
company_name: Company name
industry: Industry name
max_results: Maximum results
Returns:
Search results
"""
args = {
"company_name": company_name,
"max_results": max_results,
}
if industry:
args["industry"] = industry
return call_tool(
server_url=server_url,
tool_name="search_industry_trends",
arguments=args,
)
def extract_text_content(result: Any) -> str:
"""Extract text content from MCP tool result."""
if hasattr(result, "content"):
# Result is a list of content blocks
texts = []
for block in result.content:
if hasattr(block, "text"):
texts.append(block.text)
return "\n".join(texts)
return str(result)
def main():
"""Main function."""
parser = argparse.ArgumentParser(
description="Company Intelligence MCP Client",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Generate full report
uv run python client_example.py "トレジャーデータ"
# Search news only
uv run python client_example.py "Salesforce" --action news
# Search industry trends
uv run python client_example.py "Sony" --action trends --industry エレクトロニクス
# List available tools
uv run python client_example.py --list-tools
""",
)
parser.add_argument(
"company_name",
nargs="?",
default="トレジャーデータ",
help="Company name to research",
)
parser.add_argument(
"--host",
default="localhost",
help="MCP server host (default: localhost)",
)
parser.add_argument(
"--port",
type=int,
default=9100,
help="MCP server port (default: 9100)",
)
parser.add_argument(
"--action",
choices=["report", "news", "trends"],
default="report",
help="Action to perform (default: report)",
)
parser.add_argument(
"--industry",
help="Industry name for trends search",
)
parser.add_argument(
"--max-results",
type=int,
default=10,
help="Maximum search results (default: 10)",
)
parser.add_argument(
"--no-tracing",
action="store_true",
help="Disable Langfuse tracing",
)
parser.add_argument(
"--list-tools",
action="store_true",
help="List available MCP tools",
)
parser.add_argument(
"--health",
action="store_true",
help="Check server health",
)
parser.add_argument(
"--json",
action="store_true",
help="Output raw JSON",
)
args = parser.parse_args()
base_url = f"http://{args.host}:{args.port}"
try:
if args.health:
result = health_check(base_url)
print(json.dumps(result, indent=2, ensure_ascii=False))
return
if args.list_tools:
tools = list_tools(base_url)
print("Available MCP Tools:")
print("-" * 40)
for tool in tools:
print(f"\n{tool['name']}")
if tool.get("description"):
desc = tool["description"][:100]
print(f" {desc}...")
return
print(f"Company: {args.company_name}")
print(f"Action: {args.action}")
print(f"Server: {base_url}")
print()
if args.action == "report":
print("Generating intelligence report...")
print("(This may take a minute or two)")
print()
result = generate_report(
server_url=base_url,
company_name=args.company_name,
enable_tracing=not args.no_tracing,
)
text = extract_text_content(result)
if args.json:
# Try to parse as JSON
try:
data = json.loads(text)
print(json.dumps(data, indent=2, ensure_ascii=False))
except json.JSONDecodeError:
print(text)
else:
# Try to extract and format the report
try:
data = json.loads(text)
print("=" * 60)
print(data.get("report_markdown", text))
print("=" * 60)
if data.get("critique_score"):
print(f"\nCritique Score: {data['critique_score']}")
if data.get("iterations"):
print(f"Iterations: {data['iterations']}")
if data.get("sources_count"):
print(f"Sources: {data['sources_count']}")
except json.JSONDecodeError:
print(text)
elif args.action == "news":
print("Searching for company news...")
result = search_news(
server_url=base_url,
company_name=args.company_name,
max_results=args.max_results,
)
text = extract_text_content(result)
if args.json:
print(text)
else:
try:
data = json.loads(text)
print(f"\nFound {data.get('count', 0)} news articles:\n")
for i, news in enumerate(data.get("news", []), 1):
print(f"{i}. {news.get('title', 'No title')}")
print(f" URL: {news.get('url', '')}")
if news.get("published_date"):
print(f" Date: {news['published_date']}")
print()
except json.JSONDecodeError:
print(text)
elif args.action == "trends":
print("Searching for industry trends...")
result = search_trends(
server_url=base_url,
company_name=args.company_name,
industry=args.industry,
max_results=args.max_results,
)
text = extract_text_content(result)
if args.json:
print(text)
else:
try:
data = json.loads(text)
print(f"\nFound {data.get('count', 0)} trend articles:\n")
for i, trend in enumerate(data.get("trends", []), 1):
print(f"{i}. {trend.get('title', 'No title')}")
print(f" URL: {trend.get('url', '')}")
print()
except json.JSONDecodeError:
print(text)
except httpx.ConnectError:
print(f"Could not connect to server at {base_url}")
print("Make sure the server is running:")
print(" cd examples/mcp_server && uv run python server.py")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()