@sparkleideas/mcp 3.0.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/agents/analysis/analyze-code-quality.md +179 -0
- package/.claude/agents/analysis/code-analyzer.md +210 -0
- package/.claude/agents/analysis/code-review/analyze-code-quality.md +179 -0
- package/.claude/agents/architecture/arch-system-design.md +157 -0
- package/.claude/agents/architecture/system-design/arch-system-design.md +155 -0
- package/.claude/agents/consensus/byzantine-coordinator.md +63 -0
- package/.claude/agents/consensus/crdt-synchronizer.md +997 -0
- package/.claude/agents/consensus/gossip-coordinator.md +63 -0
- package/.claude/agents/consensus/performance-benchmarker.md +851 -0
- package/.claude/agents/consensus/quorum-manager.md +823 -0
- package/.claude/agents/consensus/raft-manager.md +63 -0
- package/.claude/agents/consensus/security-manager.md +622 -0
- package/.claude/agents/core/coder.md +453 -0
- package/.claude/agents/core/planner.md +375 -0
- package/.claude/agents/core/researcher.md +369 -0
- package/.claude/agents/core/reviewer.md +520 -0
- package/.claude/agents/core/tester.md +512 -0
- package/.claude/agents/custom/test-long-runner.md +44 -0
- package/.claude/agents/data/data-ml-model.md +445 -0
- package/.claude/agents/data/ml/data-ml-model.md +193 -0
- package/.claude/agents/development/backend/dev-backend-api.md +142 -0
- package/.claude/agents/development/dev-backend-api.md +345 -0
- package/.claude/agents/devops/ci-cd/ops-cicd-github.md +164 -0
- package/.claude/agents/devops/ops-cicd-github.md +165 -0
- package/.claude/agents/documentation/api-docs/docs-api-openapi.md +174 -0
- package/.claude/agents/documentation/docs-api-openapi.md +355 -0
- package/.claude/agents/flow-nexus/app-store.md +88 -0
- package/.claude/agents/flow-nexus/authentication.md +69 -0
- package/.claude/agents/flow-nexus/challenges.md +81 -0
- package/.claude/agents/flow-nexus/neural-network.md +88 -0
- package/.claude/agents/flow-nexus/payments.md +83 -0
- package/.claude/agents/flow-nexus/sandbox.md +76 -0
- package/.claude/agents/flow-nexus/swarm.md +76 -0
- package/.claude/agents/flow-nexus/user-tools.md +96 -0
- package/.claude/agents/flow-nexus/workflow.md +84 -0
- package/.claude/agents/github/code-review-swarm.md +377 -0
- package/.claude/agents/github/github-modes.md +173 -0
- package/.claude/agents/github/issue-tracker.md +576 -0
- package/.claude/agents/github/multi-repo-swarm.md +553 -0
- package/.claude/agents/github/pr-manager.md +438 -0
- package/.claude/agents/github/project-board-sync.md +509 -0
- package/.claude/agents/github/release-manager.md +605 -0
- package/.claude/agents/github/release-swarm.md +583 -0
- package/.claude/agents/github/repo-architect.md +398 -0
- package/.claude/agents/github/swarm-issue.md +573 -0
- package/.claude/agents/github/swarm-pr.md +428 -0
- package/.claude/agents/github/sync-coordinator.md +452 -0
- package/.claude/agents/github/workflow-automation.md +903 -0
- package/.claude/agents/goal/agent.md +816 -0
- package/.claude/agents/goal/goal-planner.md +73 -0
- package/.claude/agents/optimization/benchmark-suite.md +665 -0
- package/.claude/agents/optimization/load-balancer.md +431 -0
- package/.claude/agents/optimization/performance-monitor.md +672 -0
- package/.claude/agents/optimization/resource-allocator.md +674 -0
- package/.claude/agents/optimization/topology-optimizer.md +808 -0
- package/.claude/agents/payments/agentic-payments.md +126 -0
- package/.claude/agents/sona/sona-learning-optimizer.md +74 -0
- package/.claude/agents/sparc/architecture.md +699 -0
- package/.claude/agents/sparc/pseudocode.md +520 -0
- package/.claude/agents/sparc/refinement.md +802 -0
- package/.claude/agents/sparc/specification.md +478 -0
- package/.claude/agents/specialized/mobile/spec-mobile-react-native.md +225 -0
- package/.claude/agents/specialized/spec-mobile-react-native.md +227 -0
- package/.claude/agents/sublinear/consensus-coordinator.md +338 -0
- package/.claude/agents/sublinear/matrix-optimizer.md +185 -0
- package/.claude/agents/sublinear/pagerank-analyzer.md +299 -0
- package/.claude/agents/sublinear/performance-optimizer.md +368 -0
- package/.claude/agents/sublinear/trading-predictor.md +246 -0
- package/.claude/agents/swarm/adaptive-coordinator.md +1127 -0
- package/.claude/agents/swarm/hierarchical-coordinator.md +710 -0
- package/.claude/agents/swarm/mesh-coordinator.md +963 -0
- package/.claude/agents/templates/automation-smart-agent.md +205 -0
- package/.claude/agents/templates/base-template-generator.md +268 -0
- package/.claude/agents/templates/coordinator-swarm-init.md +90 -0
- package/.claude/agents/templates/github-pr-manager.md +177 -0
- package/.claude/agents/templates/implementer-sparc-coder.md +259 -0
- package/.claude/agents/templates/memory-coordinator.md +187 -0
- package/.claude/agents/templates/orchestrator-task.md +139 -0
- package/.claude/agents/templates/performance-analyzer.md +199 -0
- package/.claude/agents/templates/sparc-coordinator.md +514 -0
- package/.claude/agents/testing/production-validator.md +395 -0
- package/.claude/agents/testing/tdd-london-swarm.md +244 -0
- package/.claude/agents/v3/adr-architect.md +184 -0
- package/.claude/agents/v3/aidefence-guardian.md +282 -0
- package/.claude/agents/v3/claims-authorizer.md +208 -0
- package/.claude/agents/v3/collective-intelligence-coordinator.md +993 -0
- package/.claude/agents/v3/ddd-domain-expert.md +220 -0
- package/.claude/agents/v3/injection-analyst.md +236 -0
- package/.claude/agents/v3/memory-specialist.md +995 -0
- package/.claude/agents/v3/performance-engineer.md +1233 -0
- package/.claude/agents/v3/pii-detector.md +151 -0
- package/.claude/agents/v3/reasoningbank-learner.md +213 -0
- package/.claude/agents/v3/security-architect-aidefence.md +410 -0
- package/.claude/agents/v3/security-architect.md +867 -0
- package/.claude/agents/v3/security-auditor.md +771 -0
- package/.claude/agents/v3/sparc-orchestrator.md +182 -0
- package/.claude/agents/v3/swarm-memory-manager.md +157 -0
- package/.claude/agents/v3/v3-integration-architect.md +205 -0
- package/.claude/commands/analysis/COMMAND_COMPLIANCE_REPORT.md +54 -0
- package/.claude/commands/analysis/README.md +9 -0
- package/.claude/commands/analysis/bottleneck-detect.md +162 -0
- package/.claude/commands/analysis/performance-bottlenecks.md +59 -0
- package/.claude/commands/analysis/performance-report.md +25 -0
- package/.claude/commands/analysis/token-efficiency.md +45 -0
- package/.claude/commands/analysis/token-usage.md +25 -0
- package/.claude/commands/automation/README.md +9 -0
- package/.claude/commands/automation/auto-agent.md +122 -0
- package/.claude/commands/automation/self-healing.md +106 -0
- package/.claude/commands/automation/session-memory.md +90 -0
- package/.claude/commands/automation/smart-agents.md +73 -0
- package/.claude/commands/automation/smart-spawn.md +25 -0
- package/.claude/commands/automation/workflow-select.md +25 -0
- package/.claude/commands/claude-flow-help.md +103 -0
- package/.claude/commands/claude-flow-memory.md +107 -0
- package/.claude/commands/claude-flow-swarm.md +205 -0
- package/.claude/commands/github/README.md +11 -0
- package/.claude/commands/github/code-review-swarm.md +514 -0
- package/.claude/commands/github/code-review.md +25 -0
- package/.claude/commands/github/github-modes.md +147 -0
- package/.claude/commands/github/github-swarm.md +121 -0
- package/.claude/commands/github/issue-tracker.md +292 -0
- package/.claude/commands/github/issue-triage.md +25 -0
- package/.claude/commands/github/multi-repo-swarm.md +519 -0
- package/.claude/commands/github/pr-enhance.md +26 -0
- package/.claude/commands/github/pr-manager.md +170 -0
- package/.claude/commands/github/project-board-sync.md +471 -0
- package/.claude/commands/github/release-manager.md +338 -0
- package/.claude/commands/github/release-swarm.md +544 -0
- package/.claude/commands/github/repo-analyze.md +25 -0
- package/.claude/commands/github/repo-architect.md +367 -0
- package/.claude/commands/github/swarm-issue.md +482 -0
- package/.claude/commands/github/swarm-pr.md +285 -0
- package/.claude/commands/github/sync-coordinator.md +301 -0
- package/.claude/commands/github/workflow-automation.md +442 -0
- package/.claude/commands/hooks/README.md +11 -0
- package/.claude/commands/hooks/overview.md +58 -0
- package/.claude/commands/hooks/post-edit.md +117 -0
- package/.claude/commands/hooks/post-task.md +112 -0
- package/.claude/commands/hooks/pre-edit.md +113 -0
- package/.claude/commands/hooks/pre-task.md +111 -0
- package/.claude/commands/hooks/session-end.md +118 -0
- package/.claude/commands/hooks/setup.md +103 -0
- package/.claude/commands/monitoring/README.md +9 -0
- package/.claude/commands/monitoring/agent-metrics.md +25 -0
- package/.claude/commands/monitoring/agents.md +44 -0
- package/.claude/commands/monitoring/real-time-view.md +25 -0
- package/.claude/commands/monitoring/status.md +46 -0
- package/.claude/commands/monitoring/swarm-monitor.md +25 -0
- package/.claude/commands/optimization/README.md +9 -0
- package/.claude/commands/optimization/auto-topology.md +62 -0
- package/.claude/commands/optimization/cache-manage.md +25 -0
- package/.claude/commands/optimization/parallel-execute.md +25 -0
- package/.claude/commands/optimization/parallel-execution.md +50 -0
- package/.claude/commands/optimization/topology-optimize.md +25 -0
- package/.claude/commands/sparc/analyzer.md +52 -0
- package/.claude/commands/sparc/architect.md +53 -0
- package/.claude/commands/sparc/ask.md +97 -0
- package/.claude/commands/sparc/batch-executor.md +54 -0
- package/.claude/commands/sparc/code.md +89 -0
- package/.claude/commands/sparc/coder.md +54 -0
- package/.claude/commands/sparc/debug.md +83 -0
- package/.claude/commands/sparc/debugger.md +54 -0
- package/.claude/commands/sparc/designer.md +53 -0
- package/.claude/commands/sparc/devops.md +109 -0
- package/.claude/commands/sparc/docs-writer.md +80 -0
- package/.claude/commands/sparc/documenter.md +54 -0
- package/.claude/commands/sparc/innovator.md +54 -0
- package/.claude/commands/sparc/integration.md +83 -0
- package/.claude/commands/sparc/mcp.md +117 -0
- package/.claude/commands/sparc/memory-manager.md +54 -0
- package/.claude/commands/sparc/optimizer.md +54 -0
- package/.claude/commands/sparc/orchestrator.md +132 -0
- package/.claude/commands/sparc/post-deployment-monitoring-mode.md +83 -0
- package/.claude/commands/sparc/refinement-optimization-mode.md +83 -0
- package/.claude/commands/sparc/researcher.md +54 -0
- package/.claude/commands/sparc/reviewer.md +54 -0
- package/.claude/commands/sparc/security-review.md +80 -0
- package/.claude/commands/sparc/sparc-modes.md +174 -0
- package/.claude/commands/sparc/sparc.md +111 -0
- package/.claude/commands/sparc/spec-pseudocode.md +80 -0
- package/.claude/commands/sparc/supabase-admin.md +348 -0
- package/.claude/commands/sparc/swarm-coordinator.md +54 -0
- package/.claude/commands/sparc/tdd.md +54 -0
- package/.claude/commands/sparc/tester.md +54 -0
- package/.claude/commands/sparc/tutorial.md +79 -0
- package/.claude/commands/sparc/workflow-manager.md +54 -0
- package/.claude/helpers/README.md +97 -0
- package/.claude/helpers/adr-compliance.sh +186 -0
- package/.claude/helpers/auto-commit.sh +178 -0
- package/.claude/helpers/checkpoint-manager.sh +251 -0
- package/.claude/helpers/daemon-manager.sh +252 -0
- package/.claude/helpers/ddd-tracker.sh +144 -0
- package/.claude/helpers/github-safe.js +106 -0
- package/.claude/helpers/github-setup.sh +28 -0
- package/.claude/helpers/guidance-hook.sh +13 -0
- package/.claude/helpers/guidance-hooks.sh +102 -0
- package/.claude/helpers/health-monitor.sh +108 -0
- package/.claude/helpers/learning-hooks.sh +329 -0
- package/.claude/helpers/learning-optimizer.sh +127 -0
- package/.claude/helpers/learning-service.mjs +1144 -0
- package/.claude/helpers/memory.js +83 -0
- package/.claude/helpers/metrics-db.mjs +488 -0
- package/.claude/helpers/pattern-consolidator.sh +86 -0
- package/.claude/helpers/perf-worker.sh +160 -0
- package/.claude/helpers/post-commit +16 -0
- package/.claude/helpers/pre-commit +26 -0
- package/.claude/helpers/quick-start.sh +19 -0
- package/.claude/helpers/router.js +66 -0
- package/.claude/helpers/security-scanner.sh +127 -0
- package/.claude/helpers/session.js +127 -0
- package/.claude/helpers/setup-mcp.sh +18 -0
- package/.claude/helpers/standard-checkpoint-hooks.sh +189 -0
- package/.claude/helpers/statusline-hook.sh +21 -0
- package/.claude/helpers/statusline.cjs +509 -0
- package/.claude/helpers/statusline.js +316 -0
- package/.claude/helpers/swarm-comms.sh +353 -0
- package/.claude/helpers/swarm-hooks.sh +761 -0
- package/.claude/helpers/swarm-monitor.sh +211 -0
- package/.claude/helpers/sync-v3-metrics.sh +245 -0
- package/.claude/helpers/update-v3-progress.sh +166 -0
- package/.claude/helpers/v3-quick-status.sh +58 -0
- package/.claude/helpers/v3.sh +111 -0
- package/.claude/helpers/validate-v3-config.sh +216 -0
- package/.claude/helpers/worker-manager.sh +170 -0
- package/.claude/settings.json +237 -0
- package/.claude/skills/agentdb-advanced/SKILL.md +550 -0
- package/.claude/skills/agentdb-learning/SKILL.md +545 -0
- package/.claude/skills/agentdb-memory-patterns/SKILL.md +339 -0
- package/.claude/skills/agentdb-optimization/SKILL.md +509 -0
- package/.claude/skills/agentdb-vector-search/SKILL.md +339 -0
- package/.claude/skills/github-code-review/SKILL.md +1140 -0
- package/.claude/skills/github-multi-repo/SKILL.md +874 -0
- package/.claude/skills/github-project-management/SKILL.md +1277 -0
- package/.claude/skills/github-release-management/SKILL.md +1081 -0
- package/.claude/skills/github-workflow-automation/SKILL.md +1065 -0
- package/.claude/skills/hooks-automation/SKILL.md +1201 -0
- package/.claude/skills/pair-programming/SKILL.md +1202 -0
- package/.claude/skills/reasoningbank-agentdb/SKILL.md +446 -0
- package/.claude/skills/reasoningbank-intelligence/SKILL.md +201 -0
- package/.claude/skills/skill-builder/SKILL.md +910 -0
- package/.claude/skills/sparc-methodology/SKILL.md +1115 -0
- package/.claude/skills/stream-chain/SKILL.md +563 -0
- package/.claude/skills/swarm-advanced/SKILL.md +973 -0
- package/.claude/skills/swarm-orchestration/SKILL.md +179 -0
- package/.claude/skills/v3-cli-modernization/SKILL.md +872 -0
- package/.claude/skills/v3-core-implementation/SKILL.md +797 -0
- package/.claude/skills/v3-ddd-architecture/SKILL.md +442 -0
- package/.claude/skills/v3-integration-deep/SKILL.md +241 -0
- package/.claude/skills/v3-mcp-optimization/SKILL.md +777 -0
- package/.claude/skills/v3-memory-unification/SKILL.md +174 -0
- package/.claude/skills/v3-performance-optimization/SKILL.md +390 -0
- package/.claude/skills/v3-security-overhaul/SKILL.md +82 -0
- package/.claude/skills/v3-swarm-coordination/SKILL.md +340 -0
- package/.claude/skills/verification-quality/SKILL.md +649 -0
- package/CLAUDE.md +711 -0
- package/README.md +428 -0
- package/__tests__/integration.test.ts +449 -0
- package/__tests__/mcp.test.ts +641 -0
- package/package.json +44 -0
- package/src/connection-pool.ts +344 -0
- package/src/index.ts +253 -0
- package/src/oauth.ts +447 -0
- package/src/prompt-registry.ts +296 -0
- package/src/rate-limiter.ts +266 -0
- package/src/resource-registry.ts +530 -0
- package/src/sampling.ts +363 -0
- package/src/schema-validator.ts +213 -0
- package/src/server.ts +1134 -0
- package/src/session-manager.ts +339 -0
- package/src/task-manager.ts +427 -0
- package/src/tool-registry.ts +475 -0
- package/src/transport/http.ts +532 -0
- package/src/transport/index.ts +233 -0
- package/src/transport/stdio.ts +252 -0
- package/src/transport/websocket.ts +396 -0
- package/src/types.ts +664 -0
- package/tmp.json +0 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +13 -0
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sparkleideas/mcp - HTTP Transport
|
|
3
|
+
*
|
|
4
|
+
* HTTP/REST transport with WebSocket support
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { EventEmitter } from 'events';
|
|
8
|
+
import express, { Express, Request, Response, NextFunction } from 'express';
|
|
9
|
+
import { createServer, Server } from 'http';
|
|
10
|
+
import { WebSocketServer, WebSocket } from 'ws';
|
|
11
|
+
import cors from 'cors';
|
|
12
|
+
import helmet from 'helmet';
|
|
13
|
+
import type {
|
|
14
|
+
ITransport,
|
|
15
|
+
TransportType,
|
|
16
|
+
MCPRequest,
|
|
17
|
+
MCPResponse,
|
|
18
|
+
MCPNotification,
|
|
19
|
+
RequestHandler,
|
|
20
|
+
NotificationHandler,
|
|
21
|
+
TransportHealthStatus,
|
|
22
|
+
ILogger,
|
|
23
|
+
AuthConfig,
|
|
24
|
+
} from '../types.js';
|
|
25
|
+
|
|
26
|
+
export interface HttpTransportConfig {
|
|
27
|
+
host: string;
|
|
28
|
+
port: number;
|
|
29
|
+
tlsEnabled?: boolean;
|
|
30
|
+
tlsCert?: string;
|
|
31
|
+
tlsKey?: string;
|
|
32
|
+
corsEnabled?: boolean;
|
|
33
|
+
corsOrigins?: string[];
|
|
34
|
+
auth?: AuthConfig;
|
|
35
|
+
maxRequestSize?: string;
|
|
36
|
+
requestTimeout?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class HttpTransport extends EventEmitter implements ITransport {
|
|
40
|
+
public readonly type: TransportType = 'http';
|
|
41
|
+
|
|
42
|
+
private requestHandler?: RequestHandler;
|
|
43
|
+
private notificationHandler?: NotificationHandler;
|
|
44
|
+
private app: Express;
|
|
45
|
+
private server?: Server;
|
|
46
|
+
private wss?: WebSocketServer;
|
|
47
|
+
private running = false;
|
|
48
|
+
private activeConnections = new Set<WebSocket>();
|
|
49
|
+
|
|
50
|
+
private messagesReceived = 0;
|
|
51
|
+
private messagesSent = 0;
|
|
52
|
+
private errors = 0;
|
|
53
|
+
private httpRequests = 0;
|
|
54
|
+
private wsMessages = 0;
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
private readonly logger: ILogger,
|
|
58
|
+
private readonly config: HttpTransportConfig
|
|
59
|
+
) {
|
|
60
|
+
super();
|
|
61
|
+
this.app = express();
|
|
62
|
+
this.setupMiddleware();
|
|
63
|
+
this.setupRoutes();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async start(): Promise<void> {
|
|
67
|
+
if (this.running) {
|
|
68
|
+
throw new Error('HTTP transport already running');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.logger.info('Starting HTTP transport', {
|
|
72
|
+
host: this.config.host,
|
|
73
|
+
port: this.config.port,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
this.server = createServer(this.app);
|
|
77
|
+
|
|
78
|
+
this.wss = new WebSocketServer({
|
|
79
|
+
server: this.server,
|
|
80
|
+
path: '/ws',
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
this.setupWebSocketHandlers();
|
|
84
|
+
|
|
85
|
+
await new Promise<void>((resolve, reject) => {
|
|
86
|
+
this.server!.listen(this.config.port, this.config.host, () => {
|
|
87
|
+
resolve();
|
|
88
|
+
});
|
|
89
|
+
this.server!.on('error', reject);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
this.running = true;
|
|
93
|
+
this.logger.info('HTTP transport started', {
|
|
94
|
+
url: `http://${this.config.host}:${this.config.port}`,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async stop(): Promise<void> {
|
|
99
|
+
if (!this.running) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
this.logger.info('Stopping HTTP transport');
|
|
104
|
+
this.running = false;
|
|
105
|
+
|
|
106
|
+
for (const ws of this.activeConnections) {
|
|
107
|
+
try {
|
|
108
|
+
ws.close(1000, 'Server shutting down');
|
|
109
|
+
} catch {
|
|
110
|
+
// Ignore errors
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this.activeConnections.clear();
|
|
114
|
+
|
|
115
|
+
if (this.wss) {
|
|
116
|
+
this.wss.close();
|
|
117
|
+
this.wss = undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (this.server) {
|
|
121
|
+
await new Promise<void>((resolve) => {
|
|
122
|
+
this.server!.close(() => resolve());
|
|
123
|
+
});
|
|
124
|
+
this.server = undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.logger.info('HTTP transport stopped');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
onRequest(handler: RequestHandler): void {
|
|
131
|
+
this.requestHandler = handler;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
onNotification(handler: NotificationHandler): void {
|
|
135
|
+
this.notificationHandler = handler;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async getHealthStatus(): Promise<TransportHealthStatus> {
|
|
139
|
+
return {
|
|
140
|
+
healthy: this.running,
|
|
141
|
+
metrics: {
|
|
142
|
+
messagesReceived: this.messagesReceived,
|
|
143
|
+
messagesSent: this.messagesSent,
|
|
144
|
+
errors: this.errors,
|
|
145
|
+
httpRequests: this.httpRequests,
|
|
146
|
+
wsMessages: this.wsMessages,
|
|
147
|
+
activeConnections: this.activeConnections.size,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async sendNotification(notification: MCPNotification): Promise<void> {
|
|
153
|
+
const message = JSON.stringify(notification);
|
|
154
|
+
|
|
155
|
+
for (const ws of this.activeConnections) {
|
|
156
|
+
try {
|
|
157
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
158
|
+
ws.send(message);
|
|
159
|
+
this.messagesSent++;
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
this.logger.error('Failed to send notification', { error });
|
|
163
|
+
this.errors++;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private setupMiddleware(): void {
|
|
169
|
+
this.app.use(helmet({
|
|
170
|
+
contentSecurityPolicy: false,
|
|
171
|
+
}));
|
|
172
|
+
|
|
173
|
+
if (this.config.corsEnabled !== false) {
|
|
174
|
+
const allowedOrigins = this.config.corsOrigins;
|
|
175
|
+
|
|
176
|
+
if (!allowedOrigins || allowedOrigins.length === 0) {
|
|
177
|
+
this.logger.warn('CORS: No origins configured, restricting to same-origin only');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.app.use(cors({
|
|
181
|
+
origin: (origin, callback) => {
|
|
182
|
+
if (!origin) {
|
|
183
|
+
callback(null, true);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (allowedOrigins && allowedOrigins.length > 0) {
|
|
188
|
+
if (allowedOrigins.includes(origin) || allowedOrigins.includes('*')) {
|
|
189
|
+
callback(null, true);
|
|
190
|
+
} else {
|
|
191
|
+
callback(new Error(`CORS: Origin '${origin}' not allowed`));
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
callback(new Error('CORS: Cross-origin requests not allowed'));
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
credentials: true,
|
|
198
|
+
maxAge: 86400,
|
|
199
|
+
methods: ['GET', 'POST', 'OPTIONS'],
|
|
200
|
+
allowedHeaders: ['Content-Type', 'Authorization', 'X-Request-ID'],
|
|
201
|
+
}));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
this.app.use(express.json({
|
|
205
|
+
limit: this.config.maxRequestSize || '10mb',
|
|
206
|
+
}));
|
|
207
|
+
|
|
208
|
+
if (this.config.requestTimeout) {
|
|
209
|
+
this.app.use((req, res, next) => {
|
|
210
|
+
res.setTimeout(this.config.requestTimeout!, () => {
|
|
211
|
+
res.status(408).json({
|
|
212
|
+
jsonrpc: '2.0',
|
|
213
|
+
id: null,
|
|
214
|
+
error: { code: -32000, message: 'Request timeout' },
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
next();
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this.app.use((req, res, next) => {
|
|
222
|
+
const startTime = performance.now();
|
|
223
|
+
res.on('finish', () => {
|
|
224
|
+
const duration = performance.now() - startTime;
|
|
225
|
+
this.logger.debug('HTTP request', {
|
|
226
|
+
method: req.method,
|
|
227
|
+
path: req.path,
|
|
228
|
+
status: res.statusCode,
|
|
229
|
+
duration: `${duration.toFixed(2)}ms`,
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
next();
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private setupRoutes(): void {
|
|
237
|
+
this.app.get('/health', (req, res) => {
|
|
238
|
+
res.json({
|
|
239
|
+
status: 'ok',
|
|
240
|
+
timestamp: new Date().toISOString(),
|
|
241
|
+
connections: this.activeConnections.size,
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
this.app.post('/rpc', async (req, res) => {
|
|
246
|
+
await this.handleHttpRequest(req, res);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
this.app.post('/mcp', async (req, res) => {
|
|
250
|
+
await this.handleHttpRequest(req, res);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
this.app.get('/info', (req, res) => {
|
|
254
|
+
res.json({
|
|
255
|
+
name: 'Claude-Flow MCP Server V3',
|
|
256
|
+
version: '3.0.0',
|
|
257
|
+
transport: 'http',
|
|
258
|
+
capabilities: {
|
|
259
|
+
jsonrpc: true,
|
|
260
|
+
websocket: true,
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
this.app.use((req, res) => {
|
|
266
|
+
res.status(404).json({
|
|
267
|
+
error: 'Not found',
|
|
268
|
+
path: req.path,
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
this.app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
|
273
|
+
this.logger.error('Express error', { error: err });
|
|
274
|
+
this.errors++;
|
|
275
|
+
res.status(500).json({
|
|
276
|
+
jsonrpc: '2.0',
|
|
277
|
+
id: null,
|
|
278
|
+
error: { code: -32603, message: 'Internal error' },
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private setupWebSocketHandlers(): void {
|
|
284
|
+
if (!this.wss) return;
|
|
285
|
+
|
|
286
|
+
// SECURITY: Handle WebSocket authentication via upgrade request
|
|
287
|
+
this.wss.on('connection', (ws, req) => {
|
|
288
|
+
// Validate authentication if enabled
|
|
289
|
+
if (this.config.auth?.enabled) {
|
|
290
|
+
const url = new URL(req.url || '', `http://${req.headers.host}`);
|
|
291
|
+
const token = url.searchParams.get('token') || req.headers['authorization']?.replace(/^Bearer\s+/i, '');
|
|
292
|
+
|
|
293
|
+
if (!token) {
|
|
294
|
+
this.logger.warn('WebSocket connection rejected: no authentication token');
|
|
295
|
+
ws.close(4001, 'Authentication required');
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// SECURITY: Timing-safe token validation
|
|
300
|
+
let valid = false;
|
|
301
|
+
if (this.config.auth.tokens?.length) {
|
|
302
|
+
for (const validToken of this.config.auth.tokens) {
|
|
303
|
+
if (this.timingSafeCompare(token, validToken)) {
|
|
304
|
+
valid = true;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (!valid) {
|
|
311
|
+
this.logger.warn('WebSocket connection rejected: invalid token');
|
|
312
|
+
ws.close(4003, 'Invalid token');
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
this.activeConnections.add(ws);
|
|
318
|
+
this.logger.info('WebSocket client connected', {
|
|
319
|
+
total: this.activeConnections.size,
|
|
320
|
+
authenticated: !!this.config.auth?.enabled,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
ws.on('message', async (data) => {
|
|
324
|
+
await this.handleWebSocketMessage(ws, data.toString());
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
ws.on('close', () => {
|
|
328
|
+
this.activeConnections.delete(ws);
|
|
329
|
+
this.logger.info('WebSocket client disconnected', {
|
|
330
|
+
total: this.activeConnections.size,
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
ws.on('error', (error) => {
|
|
335
|
+
this.logger.error('WebSocket error', { error });
|
|
336
|
+
this.errors++;
|
|
337
|
+
this.activeConnections.delete(ws);
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private async handleHttpRequest(req: Request, res: Response): Promise<void> {
|
|
343
|
+
this.httpRequests++;
|
|
344
|
+
this.messagesReceived++;
|
|
345
|
+
|
|
346
|
+
const requiresAuth = this.config.auth?.enabled !== false;
|
|
347
|
+
|
|
348
|
+
if (requiresAuth && this.config.auth) {
|
|
349
|
+
const authResult = this.validateAuth(req);
|
|
350
|
+
if (!authResult.valid) {
|
|
351
|
+
this.logger.warn('Authentication failed', {
|
|
352
|
+
ip: req.ip,
|
|
353
|
+
path: req.path,
|
|
354
|
+
error: authResult.error,
|
|
355
|
+
});
|
|
356
|
+
res.status(401).json({
|
|
357
|
+
jsonrpc: '2.0',
|
|
358
|
+
id: null,
|
|
359
|
+
error: { code: -32001, message: 'Unauthorized' },
|
|
360
|
+
});
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
} else if (requiresAuth && !this.config.auth) {
|
|
364
|
+
this.logger.warn('No authentication configured - running in development mode');
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const message = req.body;
|
|
368
|
+
|
|
369
|
+
if (message.jsonrpc !== '2.0') {
|
|
370
|
+
res.status(400).json({
|
|
371
|
+
jsonrpc: '2.0',
|
|
372
|
+
id: message.id || null,
|
|
373
|
+
error: { code: -32600, message: 'Invalid JSON-RPC version' },
|
|
374
|
+
});
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (!message.method) {
|
|
379
|
+
res.status(400).json({
|
|
380
|
+
jsonrpc: '2.0',
|
|
381
|
+
id: message.id || null,
|
|
382
|
+
error: { code: -32600, message: 'Missing method' },
|
|
383
|
+
});
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (message.id === undefined) {
|
|
388
|
+
if (this.notificationHandler) {
|
|
389
|
+
await this.notificationHandler(message as MCPNotification);
|
|
390
|
+
}
|
|
391
|
+
res.status(204).end();
|
|
392
|
+
} else {
|
|
393
|
+
if (!this.requestHandler) {
|
|
394
|
+
res.status(500).json({
|
|
395
|
+
jsonrpc: '2.0',
|
|
396
|
+
id: message.id,
|
|
397
|
+
error: { code: -32603, message: 'No request handler' },
|
|
398
|
+
});
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
const response = await this.requestHandler(message as MCPRequest);
|
|
404
|
+
res.json(response);
|
|
405
|
+
this.messagesSent++;
|
|
406
|
+
} catch (error) {
|
|
407
|
+
this.errors++;
|
|
408
|
+
res.status(500).json({
|
|
409
|
+
jsonrpc: '2.0',
|
|
410
|
+
id: message.id,
|
|
411
|
+
error: {
|
|
412
|
+
code: -32603,
|
|
413
|
+
message: error instanceof Error ? error.message : 'Internal error',
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private async handleWebSocketMessage(ws: WebSocket, data: string): Promise<void> {
|
|
421
|
+
this.wsMessages++;
|
|
422
|
+
this.messagesReceived++;
|
|
423
|
+
|
|
424
|
+
try {
|
|
425
|
+
const message = JSON.parse(data);
|
|
426
|
+
|
|
427
|
+
if (message.jsonrpc !== '2.0') {
|
|
428
|
+
ws.send(JSON.stringify({
|
|
429
|
+
jsonrpc: '2.0',
|
|
430
|
+
id: message.id || null,
|
|
431
|
+
error: { code: -32600, message: 'Invalid JSON-RPC version' },
|
|
432
|
+
}));
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (message.id === undefined) {
|
|
437
|
+
if (this.notificationHandler) {
|
|
438
|
+
await this.notificationHandler(message as MCPNotification);
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
if (!this.requestHandler) {
|
|
442
|
+
ws.send(JSON.stringify({
|
|
443
|
+
jsonrpc: '2.0',
|
|
444
|
+
id: message.id,
|
|
445
|
+
error: { code: -32603, message: 'No request handler' },
|
|
446
|
+
}));
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const response = await this.requestHandler(message as MCPRequest);
|
|
451
|
+
ws.send(JSON.stringify(response));
|
|
452
|
+
this.messagesSent++;
|
|
453
|
+
}
|
|
454
|
+
} catch (error) {
|
|
455
|
+
this.errors++;
|
|
456
|
+
this.logger.error('WebSocket message error', { error });
|
|
457
|
+
|
|
458
|
+
try {
|
|
459
|
+
const parsed = JSON.parse(data);
|
|
460
|
+
ws.send(JSON.stringify({
|
|
461
|
+
jsonrpc: '2.0',
|
|
462
|
+
id: parsed.id || null,
|
|
463
|
+
error: { code: -32700, message: 'Parse error' },
|
|
464
|
+
}));
|
|
465
|
+
} catch {
|
|
466
|
+
ws.send(JSON.stringify({
|
|
467
|
+
jsonrpc: '2.0',
|
|
468
|
+
id: null,
|
|
469
|
+
error: { code: -32700, message: 'Parse error' },
|
|
470
|
+
}));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* SECURITY: Timing-safe token comparison to prevent timing attacks
|
|
477
|
+
*/
|
|
478
|
+
private timingSafeCompare(a: string, b: string): boolean {
|
|
479
|
+
const crypto = require('crypto');
|
|
480
|
+
|
|
481
|
+
// Ensure both strings are the same length for timing-safe comparison
|
|
482
|
+
const bufA = Buffer.from(a, 'utf-8');
|
|
483
|
+
const bufB = Buffer.from(b, 'utf-8');
|
|
484
|
+
|
|
485
|
+
// If lengths differ, still do a comparison to prevent length-based timing
|
|
486
|
+
if (bufA.length !== bufB.length) {
|
|
487
|
+
// Compare against itself to maintain constant time
|
|
488
|
+
crypto.timingSafeEqual(bufA, bufA);
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return crypto.timingSafeEqual(bufA, bufB);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
private validateAuth(req: Request): { valid: boolean; error?: string } {
|
|
496
|
+
const auth = req.headers.authorization;
|
|
497
|
+
|
|
498
|
+
if (!auth) {
|
|
499
|
+
return { valid: false, error: 'Authorization header required' };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const tokenMatch = auth.match(/^Bearer\s+(.+)$/i);
|
|
503
|
+
if (!tokenMatch) {
|
|
504
|
+
return { valid: false, error: 'Invalid authorization format' };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const token = tokenMatch[1];
|
|
508
|
+
|
|
509
|
+
if (this.config.auth?.tokens?.length) {
|
|
510
|
+
// SECURITY: Use timing-safe comparison to prevent timing attacks
|
|
511
|
+
let valid = false;
|
|
512
|
+
for (const validToken of this.config.auth.tokens) {
|
|
513
|
+
if (this.timingSafeCompare(token, validToken)) {
|
|
514
|
+
valid = true;
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (!valid) {
|
|
519
|
+
return { valid: false, error: 'Invalid token' };
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return { valid: true };
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export function createHttpTransport(
|
|
528
|
+
logger: ILogger,
|
|
529
|
+
config: HttpTransportConfig
|
|
530
|
+
): HttpTransport {
|
|
531
|
+
return new HttpTransport(logger, config);
|
|
532
|
+
}
|