Skip to content

Commit 396fb81

Browse files
niechenclaude
andcommitted
Remove old middleware and consolidate to unified event-driven tracking
This commit completes the middleware consolidation by removing deprecated components and switching to a unified event-driven architecture: **Removed old middleware classes:** - MCPMMonitoringMiddleware -> replaced by MCPMUnifiedTrackingMiddleware - MCPMUsageTrackingMiddleware -> replaced by MCPMUnifiedTrackingMiddleware **Removed deprecated database code:** - usage_sessions table and all related queries - track_session method from AccessMonitor interface - session_tracker.py module and its exports **Updated tracking approach:** - All tracking now uses events (SESSION_START/SESSION_END) with session_id linking - Usage statistics computed at query time from events table - Simplified method delegation to computed stats - Maintained backward compatibility for metadata formats **Benefits:** - Single unified middleware handles all tracking automatically - Event-driven architecture enables rich analytics - Simplified codebase with consistent patterns - Better session correlation via session_id All tests pass and usage command works correctly with computed statistics. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 19090fc commit 396fb81

File tree

13 files changed

+1387
-840
lines changed

13 files changed

+1387
-840
lines changed

CLAUDE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Claude Project Conventions
2+
3+
This file contains conventions that Claude should follow when working on this project.
4+
5+
- **Formatting:** Always format Python code with `ruff`.
6+
- **Dependency Management:** Use `uv` for all Python dependency management.
7+
- **Committing:** Always double-check with the user before committing changes to git.

src/mcpm/commands/profile/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from rich.panel import Panel
99

1010
from mcpm.fastmcp_integration.proxy import create_mcpm_proxy
11+
# Removed SessionAction import - using strings directly
1112
from mcpm.profile.profile_config import ProfileConfigManager
1213
from mcpm.utils.config import DEFAULT_PORT
1314
from mcpm.utils.logging_config import (
@@ -52,6 +53,8 @@ async def run_profile_fastmcp(profile_servers, profile_name, http_mode=False, po
5253
servers=profile_servers,
5354
name=f"profile-{profile_name}",
5455
stdio_mode=not http_mode, # stdio_mode=False for HTTP
56+
action="profile_run",
57+
profile_name=profile_name,
5558
)
5659

5760
logger.debug(f"FastMCP proxy initialized with: {[s.name for s in profile_servers]}")
@@ -62,10 +65,7 @@ async def run_profile_fastmcp(profile_servers, profile_name, http_mode=False, po
6265
# Re-suppress library logging after FastMCP initialization
6366
ensure_dependency_logging_suppressed()
6467

65-
# Record profile usage
66-
from mcpm.commands.usage import record_profile_usage
67-
68-
record_profile_usage(profile_name, "run" + ("_http" if http_mode else ""))
68+
# Note: Usage tracking is handled by proxy middleware
6969

7070
if http_mode:
7171
# Try to find an available port if the requested one is taken

src/mcpm/commands/run.py

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import asyncio
44
import logging
5-
import os
6-
import subprocess
75
import sys
86

97
import click
@@ -12,6 +10,7 @@
1210

1311
from mcpm.fastmcp_integration.proxy import create_mcpm_proxy
1412
from mcpm.global_config import GlobalConfigManager
13+
# Removed SessionAction import - using strings directly
1514
from mcpm.utils.config import DEFAULT_PORT
1615
from mcpm.utils.logging_config import (
1716
ensure_dependency_logging_suppressed,
@@ -32,76 +31,22 @@ def find_installed_server(server_name):
3231
return None, None
3332

3433

35-
def execute_server_command(server_config, server_name):
36-
"""Execute a server command with proper environment setup."""
37-
if not server_config:
38-
logger.error(f"Invalid server configuration for '{server_name}'")
39-
sys.exit(1)
40-
41-
# Get command and args from the server config
42-
command = server_config.command
43-
args = server_config.args or []
44-
45-
if not command:
46-
logger.error(f"Invalid command format for server '{server_name}'")
47-
sys.exit(1)
48-
49-
# Build the full command list
50-
full_command = [command] + args
51-
52-
# Set up environment
53-
env = os.environ.copy()
54-
55-
# Add any environment variables from server config
56-
if hasattr(server_config, "env") and server_config.env:
57-
for key, value in server_config.env.items():
58-
env[key] = str(value)
59-
60-
# Set working directory if specified
61-
cwd = getattr(server_config, "cwd", None)
62-
if cwd:
63-
cwd = os.path.expanduser(cwd)
64-
65-
try:
66-
# Record usage
67-
from mcpm.commands.usage import record_server_usage
68-
69-
record_server_usage(server_name, "run")
70-
71-
# Execute the command
72-
result = subprocess.run(full_command, env=env, cwd=cwd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
73-
74-
return result.returncode
75-
76-
except FileNotFoundError:
77-
logger.error(f"Command not found: {full_command[0]}")
78-
logger.warning("Make sure the required runtime is installed")
79-
sys.exit(1)
80-
except KeyboardInterrupt:
81-
logger.info("Server execution interrupted")
82-
logger.warning("\nServer execution interrupted")
83-
sys.exit(130)
84-
except Exception as e:
85-
logger.error(f"Error running server '{server_name}': {e}")
86-
sys.exit(1)
87-
8834

8935
async def run_server_with_fastmcp(server_config, server_name, http_mode=False, port=None):
9036
"""Run server using FastMCP proxy (stdio or HTTP)."""
9137
try:
9238
# Use default port if none specified
9339
if port is None:
9440
port = DEFAULT_PORT
95-
# Record usage
96-
from mcpm.commands.usage import record_server_usage
97-
98-
record_server_usage(server_name, "run" + ("_http" if http_mode else ""))
41+
# Note: Usage tracking is handled by proxy middleware
9942

10043
# Create FastMCP proxy for single server
44+
action = "run_http" if http_mode else "run"
10145
proxy = await create_mcpm_proxy(
10246
servers=[server_config],
10347
name=f"mcpm-run-{server_name}",
10448
stdio_mode=not http_mode, # stdio_mode=False for HTTP
49+
action=action,
10550
)
10651

10752
# Set up dependency logging for FastMCP/MCP libraries
@@ -216,7 +161,7 @@ def run(server_name, http, port):
216161
# Use FastMCP proxy for HTTP mode
217162
exit_code = asyncio.run(run_server_with_fastmcp(server_config, server_name, http_mode=True, port=port))
218163
else:
219-
# Use direct execution for stdio mode (maintains backwards compatibility)
220-
exit_code = execute_server_command(server_config, server_name)
164+
# Use FastMCP proxy for stdio mode (enables middleware and usage tracking)
165+
exit_code = asyncio.run(run_server_with_fastmcp(server_config, server_name, http_mode=False, port=port))
221166

222167
sys.exit(exit_code)

src/mcpm/commands/share.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from mcpm.core.tunnel import Tunnel
1616
from mcpm.fastmcp_integration.proxy import create_mcpm_proxy
1717
from mcpm.global_config import GlobalConfigManager
18+
# Removed SessionAction import - using strings directly
1819
from mcpm.utils.config import DEFAULT_PORT, DEFAULT_SHARE_ADDRESS
1920
from mcpm.utils.logging_config import (
2021
ensure_dependency_logging_suppressed,
@@ -81,10 +82,7 @@ async def start_fastmcp_proxy(
8182
logger.debug(f"Starting FastMCP proxy for server '{server_name}' on port {actual_port}")
8283

8384
try:
84-
# Record usage
85-
from mcpm.commands.usage import record_server_usage
86-
87-
record_server_usage(server_name, "share")
85+
# Note: Usage tracking is handled by proxy middleware
8886

8987
# Create FastMCP proxy for single server (HTTP mode for sharing)
9088
proxy = await create_mcpm_proxy(
@@ -93,6 +91,7 @@ async def start_fastmcp_proxy(
9391
stdio_mode=False, # HTTP mode for sharing
9492
auth_enabled=auth_enabled,
9593
api_key=api_key,
94+
action="share",
9695
)
9796

9897
logger.debug(f"FastMCP proxy ready on port {actual_port}")

0 commit comments

Comments
 (0)