This repository was archived by the owner on Sep 16, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathweb_client.py
More file actions
422 lines (354 loc) · 15.1 KB
/
web_client.py
File metadata and controls
422 lines (354 loc) · 15.1 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# /// script
# dependencies = [
# "fastmcp",
# "httpx",
# "logging",
# "langchain_mcp_adapters",
# "langgraph",
# "langchain_ollama",
# "typer",
# "fastapi",
# "uvicorn",
# "langchain",
# "pydantic",
# ]
# ///
"""
Copyright (c) 2025 zin mcp client developer(s) (https://github.com/zinja-coder/zin-mcp-client)
See the file 'LICENSE' for copying permission
"""
import json
import httpx
import logging
import uvicorn
from typing import List
from pydantic import BaseModel
from contextlib import AsyncExitStack
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from mcp import ClientSession
from mcp.client.stdio import stdio_client, StdioServerParameters
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent
from langchain_ollama import ChatOllama
# Setup Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Global variable to store config path
CONFIG_PATH = "mcp_config.json"
@asynccontextmanager
async def lifespan(app: FastAPI):
# start
global client
client = WebMCPClient(CONFIG_PATH)
logger.info(f"Application started with config: {CONFIG_PATH}")
yield
# Shutdown
await client.close()
# Initialize FastAPI app with lifespan
app = FastAPI(title="Zin MCP Client", version="1.0.0", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Global client instance - will be initialized with CONFIG_PATH
# TO DO Provide option to configure the path
client = None
# Serve static files
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/css", StaticFiles(directory="static/css"), name="css")
app.mount("/js", StaticFiles(directory="static/js"), name="js")
# Pydantic models for API
class QueryRequest(BaseModel):
query: str
class ServerInfo(BaseModel):
name: str
status: str
tools_count: int
class ToolInfo(BaseModel):
name: str
description: str
server: str
class ModelInfo(BaseModel):
name: str
class ServerSelectionRequest(BaseModel):
server_names: List[str]
@classmethod
def validate_to_json(cls, value):
if isinstance(value, str):
value = json.loads(value)
return value
# Web MCP Client class
class WebMCPClient:
def __init__(self, config_path: str):
self.config_path = config_path
self.config = self._load_config()
self.sessions = {}
self.tools_by_server = {}
self.llm = None
self.agent = None
self.selected_model = None
self._exit_stack = AsyncExitStack()
logger.info(f"Zin Web MCP Client initialized with config from {config_path}")
def _load_config(self) -> dict:
try:
with open(self.config_path, "r") as f:
return json.load(f)
except FileNotFoundError:
logger.warning(f"Config file not found at {self.config_path}")
return {"mcpServers": {}}
async def get_ollama_models(self) -> List[str]:
logger.info("Fetching Ollama models")
try:
async with httpx.AsyncClient() as client:
response = await client.get("http://127.0.0.1:11434/api/tags")
data = response.json()
models = [model["name"] for model in data.get("models", [])]
logger.info(f"Found {len(models)} Ollama models")
return models
except (httpx.ConnectError, httpx.RequestError) as e:
logger.error(f"Failed to connect to Ollama API: {str(e)}")
return []
async def initialize_servers(self, server_names: List [str] = None, force_reinit: bool = False) -> bool:
# Initialize or reinitialize servers with optional cleanup
if not self.config.get("mcpServers"):
logger.error("No MCP Servers configured")
return False
servers = self.config["mcpServers"]
server_names = server_names or list(servers.keys())
# Check if reinitialization is needed
current_servers = set(self.sessions.keys())
requested_servers = set(server_names)
if not force_reinit and current_servers == requested_servers:
logger.info("Servers already initialized with same configuration")
return True
# Clean up existing sessions if needed
if current_servers or force_reinit:
logger.info("Cleaning up existing sessions for reinitialization")
await self.cleanup_sessions()
self.reset_agent()
logger.info(f"Initializing Servers: {', '.join(server_names)}")
initialized_servers = 0
for server_name in server_names:
if server_name not in servers:
logger.warning(f"Server '{server_name}' not found in config")
continue
server_config = servers[server_name]
command = server_config.get("command")
args = server_config.get("args", [])
logger.info(f"Initializing {server_name}")
server_params = StdioServerParameters(command=command, args=args)
try:
server_stack = AsyncExitStack()
reader, writer = await server_stack.enter_async_context(stdio_client(server_params))
session = await server_stack.enter_async_context(ClientSession(reader, writer))
await session.initialize()
await self._exit_stack.enter_async_context(server_stack)
self.sessions[server_name] = session
tools = await load_mcp_tools(session)
self.tools_by_server[server_name] = tools
initialized_servers += 1
logger.info(f"Server {server_name} initialized with {len(tools)} tools")
except Exception as e:
logger.error(f"Error initializing {server_name}: {str(e)}")
return initialized_servers > 0
async def initialize_llm(self, model_name: str) -> bool:
logger.info(f"Initializing LLM with model: {model_name}")
try:
self.llm = ChatOllama(model=model_name)
self.selected_model = model_name
logger.info("LLM initialized successfully")
return True
except Exception as e:
logger.error(f"Error initializing LLM: {str(e)}")
return False
async def create_agent(self) -> bool:
logger.info("Creating agent with tools and LLM")
if not self.llm:
logger.error("LLM not initialized")
return False
all_tools = [tool for tools in self.tools_by_server.values() for tool in tools]
if not all_tools:
logger.error("No Tools available")
return False
try:
self.agent = create_react_agent(self.llm, all_tools)
logger.info(f"Agent created successfully with {len(all_tools)} tools")
return True
except Exception as e:
logger.error(f"Error creating agent: {str(e)}")
return False
async def run_interaction(self, query: str) -> str:
logger.info(f"Running interaction with query: {query}")
if not self.agent:
raise HTTPException(status_code=400, detail="Agent not initialized")
try:
input_data = {"messages": query}
result = await self.agent.ainvoke(input_data)
# Extract response content
if isinstance(result, dict) and "messages" in result:
msgs = result.get("messages", [])
# Look for the last non-empty AIMessage
for m in reversed(msgs):
if hasattr(m, "__class__") and m.__class__.__name__ == "AIMessage":
content = getattr(m, "content", "").strip()
if content:
return content
# Fall back to tool messages
for m in reversed(msgs):
if hasattr(m, "__class__") and m.__class__.__name__ == "ToolMessage":
content = getattr(m, "content", "").strip()
if content:
return content
return "No response content received from LLM"
else:
return str(result)
except Exception as e:
logger.error(f"Error during interaction: {str(e)}")
if "does not support tools" in str(e):
raise HTTPException(status_code=400, detail="This model does not support tool calling")
else:
raise HTTPException(status_code=500, detail=f"Error during interaction: {str(e)}")
def get_servers_info(self) -> List[ServerInfo]:
servers_info = []
for server_name in self.sessions:
tools_count = len(self.tools_by_server.get(server_name, []))
servers_info.append(ServerInfo(
name=server_name,
status="Connected",
tools_count=tools_count
))
return servers_info
def get_tools_info(self) -> List[ToolInfo]:
tools_info = []
for server_name, tools in self.tools_by_server.items():
for tool in tools:
tools_info.append(ToolInfo(
name=tool.name,
description=tool.description,
server=server_name
))
return tools_info
async def cleanup_sessions(self, server_names_to_keep: List[str] = None):
# Clean up existing sessions, optionally keeping only specified servers
logger.info("Cleaning up existing MCP server sessions")
if server_names_to_keep is None:
# Close all sessions
server_to_remove = list(self.sessions.keys())
else:
# Close only sessions not in the keep list
servers_to_remove = [name for name in self.sessions.keys() if name not in server_names_to_keep]
try:
for server_name in servers_to_remove:
if server_name in self.sessions:
try:
# Simply remove from our tracking - let the exit stack handle cleanup
del self.sessions[server_name]
if server_name in self.tools_by_server:
del self.tools_by_server[server_name]
logger.info(f"Cleaned up session for server: {server_name}")
except Exception as e:
logger.error(f"Error cleaning up session for {server_name}: {str(e)}")
except UnboundLocalError:
# this error will be produced when user reloads the web page and re initializes the server so ignore
pass
# Only close the exit stack if removing all servers
if server_names_to_keep is None or len(servers_to_remove) == len(self.sessions):
try:
await self._exit_stack.aclose()
except Exception as e:
logger.error(f"Error closing exit stack: {str(e)}")
finally:
self._exit_stack = AsyncExitStack()
def reset_agent(self):
# Reset the agent to None
logger.info("Resetting agent")
self.agent = None
async def close(self):
logger.info("Closing all MCP server connections")
try:
await self._exit_stack.aclose()
except Exception as e:
logger.error(f"Error during cleanup: {str(e)}")
# Routes
@app.get("/", response_class=HTMLResponse)
async def get_homepage():
# Serve the main HTML file
try:
with open("static/index.html", "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read())
except FileNotFoundError:
raise HTTPException(status_code=404, detail="index.html not found")
@app.get("/api/models")
async def get_models():
models = await client.get_ollama_models()
return [ModelInfo(name=model) for model in models]
@app.post("/api/initialize-llm")
async def initialize_llm(request: dict):
model = request.get("model")
if not model:
raise HTTPException(status_code=400, detail="Model name is required")
success = await client.initialize_llm(model)
if not success:
raise HTTPException(status_code=500, detail="Failed to initialize LLM")
return {"status": "success", "message": f"LLM to initialized with model: {model}"}
@app.get("/api/available-servers")
async def get_available_servers():
# Get list of available MCP Servers from config
servers = list(client.config.get("mcpServers", {}).keys())
return {"servers": servers}
@app.post("/api/create-agent")
async def create_agent():
success = await client.create_agent()
if not success:
raise HTTPException(status_code=500, detail="Failed to create agent")
return {"status": "success", "message": "Agent created successfully"}
@app.post("/api/initialize-servers")
async def initialize_servers_endpoint(request: ServerSelectionRequest = None):
# Initialize or reinitialize MCP Servers
server_names = None
force_reinit = False
if request:
server_names = request.server_names
# If servers are already running, force reinitialization
force_reinit = bool(client.sessions)
success = await client.initialize_servers(server_names, force_reinit)
if not success:
action = "reinitialize" if force_reinit else "initialize"
raise HTTPException(status_code=500, detail=f"Failed to {action} selected MCP servers")
selected_servers = server_names or list(client.config.get("mcpServers", {}).keys())
action = "reinitialized" if force_reinit else "initialized"
return {
"status": "success",
"message": f"MCP servers {action}: {', '.join(selected_servers)}",
"initialized_servers": selected_servers,
"action": action
}
@app.post("/api/query")
async def process_query(request: QueryRequest):
response = await client.run_interaction(request.query)
return {"response": response}
@app.get("/api/servers")
async def get_servers():
return client.get_servers_info()
@app.get("/api/tools")
async def get_tools():
return client.get_tools_info()
@app.get("/api/status")
async def get_system_status():
# Get current system status
return {
"llm_initialized": client.llm is not None,
"selected_model": client.selected_model,
"agent_created": client.agent is not None,
"active_servers": list(client.sessions.keys()),
"total_tools": sum(len(tools) for tools in client.tools_by_server.values())
}
if __name__ == "__main__":
uvicorn.run(app)