@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,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sparkleideas/mcp - Prompt Registry
|
|
3
|
+
*
|
|
4
|
+
* MCP 2025-11-25 compliant prompt management
|
|
5
|
+
* Supports: list, get, arguments, templates, embedded resources
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { EventEmitter } from 'events';
|
|
9
|
+
import type {
|
|
10
|
+
MCPPrompt,
|
|
11
|
+
PromptArgument,
|
|
12
|
+
PromptMessage,
|
|
13
|
+
PromptListResult,
|
|
14
|
+
PromptGetResult,
|
|
15
|
+
PromptContent,
|
|
16
|
+
TextContent,
|
|
17
|
+
EmbeddedResource,
|
|
18
|
+
ResourceContent,
|
|
19
|
+
ILogger,
|
|
20
|
+
} from './types.js';
|
|
21
|
+
|
|
22
|
+
export type PromptHandler = (
|
|
23
|
+
args: Record<string, string>
|
|
24
|
+
) => Promise<PromptMessage[]>;
|
|
25
|
+
|
|
26
|
+
export interface PromptDefinition extends MCPPrompt {
|
|
27
|
+
handler: PromptHandler;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface PromptRegistryOptions {
|
|
31
|
+
maxPrompts?: number;
|
|
32
|
+
validateArguments?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class PromptRegistry extends EventEmitter {
|
|
36
|
+
private prompts: Map<string, PromptDefinition> = new Map();
|
|
37
|
+
|
|
38
|
+
private readonly options: Required<PromptRegistryOptions>;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly logger: ILogger,
|
|
42
|
+
options: PromptRegistryOptions = {}
|
|
43
|
+
) {
|
|
44
|
+
super();
|
|
45
|
+
this.options = {
|
|
46
|
+
maxPrompts: options.maxPrompts ?? 1000,
|
|
47
|
+
validateArguments: options.validateArguments ?? true,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Register a prompt
|
|
53
|
+
*/
|
|
54
|
+
register(prompt: PromptDefinition): boolean {
|
|
55
|
+
if (this.prompts.size >= this.options.maxPrompts) {
|
|
56
|
+
this.logger.error('Maximum prompts reached', { max: this.options.maxPrompts });
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (this.prompts.has(prompt.name)) {
|
|
61
|
+
this.logger.warn('Prompt already registered', { name: prompt.name });
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
this.prompts.set(prompt.name, prompt);
|
|
66
|
+
|
|
67
|
+
this.logger.debug('Prompt registered', { name: prompt.name });
|
|
68
|
+
this.emit('prompt:registered', { name: prompt.name });
|
|
69
|
+
this.emitListChanged();
|
|
70
|
+
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Unregister a prompt
|
|
76
|
+
*/
|
|
77
|
+
unregister(name: string): boolean {
|
|
78
|
+
if (!this.prompts.has(name)) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.prompts.delete(name);
|
|
83
|
+
|
|
84
|
+
this.logger.debug('Prompt unregistered', { name });
|
|
85
|
+
this.emit('prompt:unregistered', { name });
|
|
86
|
+
this.emitListChanged();
|
|
87
|
+
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* List prompts with pagination
|
|
93
|
+
*/
|
|
94
|
+
list(cursor?: string, pageSize: number = 50): PromptListResult {
|
|
95
|
+
const allPrompts = Array.from(this.prompts.values()).map((p) => ({
|
|
96
|
+
name: p.name,
|
|
97
|
+
title: p.title,
|
|
98
|
+
description: p.description,
|
|
99
|
+
arguments: p.arguments,
|
|
100
|
+
}));
|
|
101
|
+
|
|
102
|
+
let startIndex = 0;
|
|
103
|
+
if (cursor) {
|
|
104
|
+
const decoded = this.decodeCursor(cursor);
|
|
105
|
+
startIndex = decoded.offset;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const endIndex = Math.min(startIndex + pageSize, allPrompts.length);
|
|
109
|
+
const prompts = allPrompts.slice(startIndex, endIndex);
|
|
110
|
+
|
|
111
|
+
const result: PromptListResult = { prompts };
|
|
112
|
+
|
|
113
|
+
if (endIndex < allPrompts.length) {
|
|
114
|
+
result.nextCursor = this.encodeCursor({ offset: endIndex });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Get a prompt with arguments
|
|
122
|
+
*/
|
|
123
|
+
async get(
|
|
124
|
+
name: string,
|
|
125
|
+
args: Record<string, string> = {}
|
|
126
|
+
): Promise<PromptGetResult> {
|
|
127
|
+
const prompt = this.prompts.get(name);
|
|
128
|
+
if (!prompt) {
|
|
129
|
+
throw new Error(`Prompt not found: ${name}`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Validate required arguments
|
|
133
|
+
if (this.options.validateArguments && prompt.arguments) {
|
|
134
|
+
for (const arg of prompt.arguments) {
|
|
135
|
+
if (arg.required && !(arg.name in args)) {
|
|
136
|
+
throw new Error(`Missing required argument: ${arg.name}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const messages = await prompt.handler(args);
|
|
142
|
+
|
|
143
|
+
this.emit('prompt:get', { name, argCount: Object.keys(args).length });
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
description: prompt.description,
|
|
147
|
+
messages,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Get prompt by name
|
|
153
|
+
*/
|
|
154
|
+
getPrompt(name: string): MCPPrompt | undefined {
|
|
155
|
+
const prompt = this.prompts.get(name);
|
|
156
|
+
if (!prompt) return undefined;
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
name: prompt.name,
|
|
160
|
+
title: prompt.title,
|
|
161
|
+
description: prompt.description,
|
|
162
|
+
arguments: prompt.arguments,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Check if prompt exists
|
|
168
|
+
*/
|
|
169
|
+
hasPrompt(name: string): boolean {
|
|
170
|
+
return this.prompts.has(name);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get prompt count
|
|
175
|
+
*/
|
|
176
|
+
getPromptCount(): number {
|
|
177
|
+
return this.prompts.size;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Get stats
|
|
182
|
+
*/
|
|
183
|
+
getStats(): {
|
|
184
|
+
totalPrompts: number;
|
|
185
|
+
promptsWithArgs: number;
|
|
186
|
+
} {
|
|
187
|
+
let promptsWithArgs = 0;
|
|
188
|
+
for (const prompt of this.prompts.values()) {
|
|
189
|
+
if (prompt.arguments && prompt.arguments.length > 0) {
|
|
190
|
+
promptsWithArgs++;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
totalPrompts: this.prompts.size,
|
|
196
|
+
promptsWithArgs,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Encode cursor for pagination
|
|
202
|
+
*/
|
|
203
|
+
private encodeCursor(data: { offset: number }): string {
|
|
204
|
+
return Buffer.from(JSON.stringify(data)).toString('base64');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Decode cursor for pagination
|
|
209
|
+
*/
|
|
210
|
+
private decodeCursor(cursor: string): { offset: number } {
|
|
211
|
+
try {
|
|
212
|
+
return JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8'));
|
|
213
|
+
} catch {
|
|
214
|
+
return { offset: 0 };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Emit listChanged notification
|
|
220
|
+
*/
|
|
221
|
+
private emitListChanged(): void {
|
|
222
|
+
this.emit('prompts:listChanged');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function createPromptRegistry(
|
|
227
|
+
logger: ILogger,
|
|
228
|
+
options?: PromptRegistryOptions
|
|
229
|
+
): PromptRegistry {
|
|
230
|
+
return new PromptRegistry(logger, options);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Helper to define a prompt
|
|
235
|
+
*/
|
|
236
|
+
export function definePrompt(
|
|
237
|
+
name: string,
|
|
238
|
+
description: string,
|
|
239
|
+
handler: PromptHandler,
|
|
240
|
+
options?: {
|
|
241
|
+
title?: string;
|
|
242
|
+
arguments?: PromptArgument[];
|
|
243
|
+
}
|
|
244
|
+
): PromptDefinition {
|
|
245
|
+
return {
|
|
246
|
+
name,
|
|
247
|
+
description,
|
|
248
|
+
title: options?.title,
|
|
249
|
+
arguments: options?.arguments,
|
|
250
|
+
handler,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Helper to create a text message
|
|
256
|
+
*/
|
|
257
|
+
export function textMessage(
|
|
258
|
+
role: 'user' | 'assistant',
|
|
259
|
+
text: string
|
|
260
|
+
): PromptMessage {
|
|
261
|
+
return {
|
|
262
|
+
role,
|
|
263
|
+
content: {
|
|
264
|
+
type: 'text',
|
|
265
|
+
text,
|
|
266
|
+
} as TextContent,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Helper to create a message with embedded resource
|
|
272
|
+
*/
|
|
273
|
+
export function resourceMessage(
|
|
274
|
+
role: 'user' | 'assistant',
|
|
275
|
+
resource: ResourceContent
|
|
276
|
+
): PromptMessage {
|
|
277
|
+
return {
|
|
278
|
+
role,
|
|
279
|
+
content: {
|
|
280
|
+
type: 'resource',
|
|
281
|
+
resource,
|
|
282
|
+
} as EmbeddedResource,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Template string interpolation for prompts
|
|
288
|
+
*/
|
|
289
|
+
export function interpolate(
|
|
290
|
+
template: string,
|
|
291
|
+
args: Record<string, string>
|
|
292
|
+
): string {
|
|
293
|
+
return template.replace(/\{(\w+)\}/g, (match, key) => {
|
|
294
|
+
return args[key] ?? match;
|
|
295
|
+
});
|
|
296
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sparkleideas/mcp - Rate Limiter
|
|
3
|
+
*
|
|
4
|
+
* Token bucket rate limiting for DoS protection
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { EventEmitter } from 'events';
|
|
8
|
+
import type { ILogger } from './types.js';
|
|
9
|
+
|
|
10
|
+
export interface RateLimitConfig {
|
|
11
|
+
/** Requests per second */
|
|
12
|
+
requestsPerSecond: number;
|
|
13
|
+
/** Burst size (max tokens) */
|
|
14
|
+
burstSize: number;
|
|
15
|
+
/** Per-session limits (if different from global) */
|
|
16
|
+
perSessionLimit?: number;
|
|
17
|
+
/** Cleanup interval for expired sessions */
|
|
18
|
+
cleanupInterval?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface RateLimitResult {
|
|
22
|
+
allowed: boolean;
|
|
23
|
+
remaining: number;
|
|
24
|
+
resetIn: number; // ms until bucket refills
|
|
25
|
+
retryAfter?: number; // seconds to wait (for 429 response)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface TokenBucket {
|
|
29
|
+
tokens: number;
|
|
30
|
+
lastRefill: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DEFAULT_CONFIG: RateLimitConfig = {
|
|
34
|
+
requestsPerSecond: 100,
|
|
35
|
+
burstSize: 200,
|
|
36
|
+
perSessionLimit: 50,
|
|
37
|
+
cleanupInterval: 60000, // 1 minute
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export class RateLimiter extends EventEmitter {
|
|
41
|
+
private readonly config: Required<RateLimitConfig>;
|
|
42
|
+
private globalBucket: TokenBucket;
|
|
43
|
+
private sessionBuckets: Map<string, TokenBucket> = new Map();
|
|
44
|
+
private cleanupTimer?: NodeJS.Timeout;
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
private readonly logger: ILogger,
|
|
48
|
+
config: Partial<RateLimitConfig> = {}
|
|
49
|
+
) {
|
|
50
|
+
super();
|
|
51
|
+
this.config = { ...DEFAULT_CONFIG, ...config } as Required<RateLimitConfig>;
|
|
52
|
+
|
|
53
|
+
// Initialize global bucket
|
|
54
|
+
this.globalBucket = {
|
|
55
|
+
tokens: this.config.burstSize,
|
|
56
|
+
lastRefill: Date.now(),
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
this.startCleanup();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Check if request is allowed (global limit)
|
|
64
|
+
*/
|
|
65
|
+
checkGlobal(): RateLimitResult {
|
|
66
|
+
return this.checkBucket(this.globalBucket, this.config.requestsPerSecond, this.config.burstSize);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if request is allowed (per-session limit)
|
|
71
|
+
*/
|
|
72
|
+
checkSession(sessionId: string): RateLimitResult {
|
|
73
|
+
let bucket = this.sessionBuckets.get(sessionId);
|
|
74
|
+
if (!bucket) {
|
|
75
|
+
bucket = {
|
|
76
|
+
tokens: this.config.perSessionLimit,
|
|
77
|
+
lastRefill: Date.now(),
|
|
78
|
+
};
|
|
79
|
+
this.sessionBuckets.set(sessionId, bucket);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return this.checkBucket(
|
|
83
|
+
bucket,
|
|
84
|
+
this.config.perSessionLimit / 10, // Refill rate (10 seconds to full)
|
|
85
|
+
this.config.perSessionLimit
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Check both global and session limits
|
|
91
|
+
*/
|
|
92
|
+
check(sessionId?: string): RateLimitResult {
|
|
93
|
+
const globalResult = this.checkGlobal();
|
|
94
|
+
if (!globalResult.allowed) {
|
|
95
|
+
this.emit('rate-limit:global', { remaining: globalResult.remaining });
|
|
96
|
+
return globalResult;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (sessionId) {
|
|
100
|
+
const sessionResult = this.checkSession(sessionId);
|
|
101
|
+
if (!sessionResult.allowed) {
|
|
102
|
+
this.emit('rate-limit:session', { sessionId, remaining: sessionResult.remaining });
|
|
103
|
+
return sessionResult;
|
|
104
|
+
}
|
|
105
|
+
return sessionResult;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return globalResult;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Consume a token (call after request is processed)
|
|
113
|
+
*/
|
|
114
|
+
consume(sessionId?: string): void {
|
|
115
|
+
this.consumeFromBucket(this.globalBucket);
|
|
116
|
+
if (sessionId) {
|
|
117
|
+
const bucket = this.sessionBuckets.get(sessionId);
|
|
118
|
+
if (bucket) {
|
|
119
|
+
this.consumeFromBucket(bucket);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Reset session bucket
|
|
126
|
+
*/
|
|
127
|
+
resetSession(sessionId: string): void {
|
|
128
|
+
this.sessionBuckets.delete(sessionId);
|
|
129
|
+
this.logger.debug('Rate limit session reset', { sessionId });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Get current stats
|
|
134
|
+
*/
|
|
135
|
+
getStats(): {
|
|
136
|
+
globalTokens: number;
|
|
137
|
+
globalBurstSize: number;
|
|
138
|
+
sessionCount: number;
|
|
139
|
+
config: RateLimitConfig;
|
|
140
|
+
} {
|
|
141
|
+
this.refillBucket(this.globalBucket, this.config.requestsPerSecond, this.config.burstSize);
|
|
142
|
+
return {
|
|
143
|
+
globalTokens: Math.floor(this.globalBucket.tokens),
|
|
144
|
+
globalBurstSize: this.config.burstSize,
|
|
145
|
+
sessionCount: this.sessionBuckets.size,
|
|
146
|
+
config: this.config,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Destroy the rate limiter
|
|
152
|
+
*/
|
|
153
|
+
destroy(): void {
|
|
154
|
+
if (this.cleanupTimer) {
|
|
155
|
+
clearInterval(this.cleanupTimer);
|
|
156
|
+
this.cleanupTimer = undefined;
|
|
157
|
+
}
|
|
158
|
+
this.sessionBuckets.clear();
|
|
159
|
+
this.removeAllListeners();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Check bucket and refill tokens
|
|
164
|
+
*/
|
|
165
|
+
private checkBucket(
|
|
166
|
+
bucket: TokenBucket,
|
|
167
|
+
refillRate: number,
|
|
168
|
+
maxTokens: number
|
|
169
|
+
): RateLimitResult {
|
|
170
|
+
this.refillBucket(bucket, refillRate, maxTokens);
|
|
171
|
+
|
|
172
|
+
if (bucket.tokens >= 1) {
|
|
173
|
+
return {
|
|
174
|
+
allowed: true,
|
|
175
|
+
remaining: Math.floor(bucket.tokens) - 1,
|
|
176
|
+
resetIn: Math.ceil((maxTokens - bucket.tokens) / refillRate * 1000),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Calculate when bucket will have 1 token
|
|
181
|
+
const tokensNeeded = 1 - bucket.tokens;
|
|
182
|
+
const retryAfter = Math.ceil(tokensNeeded / refillRate);
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
allowed: false,
|
|
186
|
+
remaining: 0,
|
|
187
|
+
resetIn: retryAfter * 1000,
|
|
188
|
+
retryAfter,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Refill tokens based on elapsed time
|
|
194
|
+
*/
|
|
195
|
+
private refillBucket(bucket: TokenBucket, refillRate: number, maxTokens: number): void {
|
|
196
|
+
const now = Date.now();
|
|
197
|
+
const elapsed = (now - bucket.lastRefill) / 1000; // seconds
|
|
198
|
+
const tokensToAdd = elapsed * refillRate;
|
|
199
|
+
|
|
200
|
+
bucket.tokens = Math.min(maxTokens, bucket.tokens + tokensToAdd);
|
|
201
|
+
bucket.lastRefill = now;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Consume a token from bucket
|
|
206
|
+
*/
|
|
207
|
+
private consumeFromBucket(bucket: TokenBucket): void {
|
|
208
|
+
if (bucket.tokens >= 1) {
|
|
209
|
+
bucket.tokens -= 1;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Start cleanup timer for expired sessions
|
|
215
|
+
*/
|
|
216
|
+
private startCleanup(): void {
|
|
217
|
+
this.cleanupTimer = setInterval(() => {
|
|
218
|
+
const now = Date.now();
|
|
219
|
+
const expireTime = this.config.cleanupInterval * 2;
|
|
220
|
+
|
|
221
|
+
for (const [sessionId, bucket] of this.sessionBuckets) {
|
|
222
|
+
if (now - bucket.lastRefill > expireTime) {
|
|
223
|
+
this.sessionBuckets.delete(sessionId);
|
|
224
|
+
this.logger.debug('Rate limit session expired', { sessionId });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}, this.config.cleanupInterval);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function createRateLimiter(
|
|
232
|
+
logger: ILogger,
|
|
233
|
+
config?: Partial<RateLimitConfig>
|
|
234
|
+
): RateLimiter {
|
|
235
|
+
return new RateLimiter(logger, config);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Express/Connect middleware for rate limiting
|
|
240
|
+
*/
|
|
241
|
+
export function rateLimitMiddleware(rateLimiter: RateLimiter) {
|
|
242
|
+
return (req: any, res: any, next: () => void) => {
|
|
243
|
+
const sessionId = req.headers['x-session-id'] || req.ip;
|
|
244
|
+
const result = rateLimiter.check(sessionId);
|
|
245
|
+
|
|
246
|
+
res.setHeader('X-RateLimit-Remaining', result.remaining);
|
|
247
|
+
res.setHeader('X-RateLimit-Reset', Math.ceil(Date.now() / 1000) + Math.ceil(result.resetIn / 1000));
|
|
248
|
+
|
|
249
|
+
if (!result.allowed) {
|
|
250
|
+
res.setHeader('Retry-After', result.retryAfter);
|
|
251
|
+
res.status(429).json({
|
|
252
|
+
jsonrpc: '2.0',
|
|
253
|
+
id: null,
|
|
254
|
+
error: {
|
|
255
|
+
code: -32000,
|
|
256
|
+
message: 'Rate limit exceeded',
|
|
257
|
+
data: { retryAfter: result.retryAfter },
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
rateLimiter.consume(sessionId);
|
|
264
|
+
next();
|
|
265
|
+
};
|
|
266
|
+
}
|