Compare commits

..

2 Commits

5 changed files with 35 additions and 35 deletions

View File

@@ -1,13 +1,13 @@
repos: repos:
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.11 rev: v0.11.2
hooks: hooks:
- id: ruff - id: ruff
args: [--fix] args: [--fix]
- id: ruff-format - id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 rev: v5.0.0
hooks: hooks:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer

View File

@@ -7,6 +7,6 @@
"--db-path", "--db-path",
"~/.dolphin/dolphin.db" "~/.dolphin/dolphin.db"
] ]
} }
} }
} }

View File

@@ -29,7 +29,7 @@ graph TD
A[app.py] --> B[OpenAIClient] A[app.py] --> B[OpenAIClient]
B --> C[config.ini] B --> C[config.ini]
A --> D[Streamlit Components] A --> D[Streamlit Components]
style A fill:#4CAF50 style A fill:#4CAF50
style B fill:#2196F3 style B fill:#2196F3
style C fill:#FF9800 style C fill:#FF9800

View File

@@ -29,27 +29,27 @@ The OpenAIClient will parse this into a format Dolphin MCP can use:
def parse_mcp_config(self): def parse_mcp_config(self):
if not self.config.has_section('mcp'): if not self.config.has_section('mcp'):
return None return None
mcp_config = {"mcpServers": {}, "models": []} mcp_config = {"mcpServers": {}, "models": []}
# Check if MCP is enabled # Check if MCP is enabled
if not self.config['mcp'].getboolean('enabled', False): if not self.config['mcp'].getboolean('enabled', False):
return None return None
# Parse server configurations # Parse server configurations
for key in self.config['mcp']: for key in self.config['mcp']:
if key.startswith('server.') and '.' in key[7:]: if key.startswith('server.') and '.' in key[7:]:
parts = key.split('.') parts = key.split('.')
server_name = parts[1] server_name = parts[1]
config_key = '.'.join(parts[2:]) config_key = '.'.join(parts[2:])
if server_name not in mcp_config["mcpServers"]: if server_name not in mcp_config["mcpServers"]:
mcp_config["mcpServers"][server_name] = { mcp_config["mcpServers"][server_name] = {
"command": "", "command": "",
"args": [], "args": [],
"env": {} "env": {}
} }
if config_key == 'command': if config_key == 'command':
mcp_config["mcpServers"][server_name]["command"] = self.config['mcp'][key] mcp_config["mcpServers"][server_name]["command"] = self.config['mcp'][key]
elif config_key == 'args': elif config_key == 'args':
@@ -57,7 +57,7 @@ def parse_mcp_config(self):
elif config_key.startswith('env.'): elif config_key.startswith('env.'):
env_key = config_key[4:] env_key = config_key[4:]
mcp_config["mcpServers"][server_name]["env"][env_key] = self.config['mcp'][key] mcp_config["mcpServers"][server_name]["env"][env_key] = self.config['mcp'][key]
# Add model configuration from existing OpenAI settings # Add model configuration from existing OpenAI settings
model_config = { model_config = {
"model": self.config['openai']['model'], "model": self.config['openai']['model'],
@@ -67,7 +67,7 @@ def parse_mcp_config(self):
"systemMessage": "You are a helpful assistant that can use tools." "systemMessage": "You are a helpful assistant that can use tools."
} }
mcp_config["models"].append(model_config) mcp_config["models"].append(model_config)
return mcp_config return mcp_config
``` ```
@@ -92,7 +92,7 @@ def __init__(self):
```python ```python
async def get_chat_response(self, messages): async def get_chat_response(self, messages):
mcp_config = self.parse_mcp_config() mcp_config = self.parse_mcp_config()
if not mcp_config: if not mcp_config:
# Fall back to standard OpenAI if MCP not enabled # Fall back to standard OpenAI if MCP not enabled
return self.client.chat.completions.create( return self.client.chat.completions.create(
@@ -100,7 +100,7 @@ async def get_chat_response(self, messages):
messages=messages, messages=messages,
stream=True stream=True
) )
# Use Dolphin MCP with our parsed config # Use Dolphin MCP with our parsed config
return run_interaction( return run_interaction(
user_query=messages[-1]["content"], user_query=messages[-1]["content"],
@@ -121,33 +121,33 @@ from dolphin_mcp import MCPClient, run_interaction
class SyncMCPManager: class SyncMCPManager:
"""Synchronous wrapper for MCP server management""" """Synchronous wrapper for MCP server management"""
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.servers = {} self.servers = {}
self.initialized = False self.initialized = False
self._lock = threading.Lock() self._lock = threading.Lock()
def initialize(self): def initialize(self):
"""Initialize and start all MCP servers synchronously""" """Initialize and start all MCP servers synchronously"""
if self.initialized: if self.initialized:
return True return True
with self._lock: with self._lock:
if self.initialized: # Double-check after acquiring lock if self.initialized: # Double-check after acquiring lock
return True return True
if not self.config or "mcpServers" not in self.config: if not self.config or "mcpServers" not in self.config:
return False return False
# Run the async initialization in a synchronous wrapper # Run the async initialization in a synchronous wrapper
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
success = loop.run_until_complete(self._async_initialize()) success = loop.run_until_complete(self._async_initialize())
loop.close() loop.close()
self.initialized = success self.initialized = success
return success return success
async def _async_initialize(self): async def _async_initialize(self):
"""Async implementation of server initialization""" """Async implementation of server initialization"""
success = True success = True
@@ -158,7 +158,7 @@ class SyncMCPManager:
args=server_config.get("args", []), args=server_config.get("args", []),
env=server_config.get("env", {}) env=server_config.get("env", {})
) )
ok = await client.start() ok = await client.start()
if ok: if ok:
# Get available tools # Get available tools
@@ -170,44 +170,44 @@ class SyncMCPManager:
else: else:
success = False success = False
print(f"Failed to start MCP server: {server_name}") print(f"Failed to start MCP server: {server_name}")
return success return success
def shutdown(self): def shutdown(self):
"""Shut down all MCP servers synchronously""" """Shut down all MCP servers synchronously"""
if not self.initialized: if not self.initialized:
return return
with self._lock: with self._lock:
if not self.initialized: if not self.initialized:
return return
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
loop.run_until_complete(self._async_shutdown()) loop.run_until_complete(self._async_shutdown())
loop.close() loop.close()
self.servers = {} self.servers = {}
self.initialized = False self.initialized = False
async def _async_shutdown(self): async def _async_shutdown(self):
"""Async implementation of server shutdown""" """Async implementation of server shutdown"""
for server_info in self.servers.values(): for server_info in self.servers.values():
await server_info["client"].stop() await server_info["client"].stop()
def process_query(self, query, model_name=None): def process_query(self, query, model_name=None):
"""Process a query using MCP tools synchronously""" """Process a query using MCP tools synchronously"""
if not self.initialized: if not self.initialized:
self.initialize() self.initialize()
if not self.initialized: if not self.initialized:
return {"error": "Failed to initialize MCP servers"} return {"error": "Failed to initialize MCP servers"}
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
result = loop.run_until_complete(self._async_process_query(query, model_name)) result = loop.run_until_complete(self._async_process_query(query, model_name))
loop.close() loop.close()
return result return result
async def _async_process_query(self, query, model_name=None): async def _async_process_query(self, query, model_name=None):
"""Async implementation of query processing""" """Async implementation of query processing"""
return await run_interaction( return await run_interaction(
@@ -249,7 +249,7 @@ def handle_user_input():
if prompt := st.chat_input("Type your message..."): if prompt := st.chat_input("Type your message..."):
if mcp_manager and mcp_manager.initialized: if mcp_manager and mcp_manager.initialized:
response = mcp_manager.process_query( response = mcp_manager.process_query(
prompt, prompt,
model_name=client.config['openai']['model'] model_name=client.config['openai']['model']
) )
# Handle response... # Handle response...

View File

@@ -33,7 +33,7 @@ dev = [
"pytest>=8.3.4", "pytest>=8.3.4",
"pytest-asyncio>=0.25.0", "pytest-asyncio>=0.25.0",
"pytest-mock>=3.14.0", "pytest-mock>=3.14.0",
"ruff>=0.9.2" "ruff"
] ]
[build-system] [build-system]