@gotza02/seq-thinking 1.1.0
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/README.md +149 -0
- package/SYSTEM_INSTRUCTIONS.md +50 -0
- package/agents_test.log +15 -0
- package/dist/__tests__/agents.test.d.ts +2 -0
- package/dist/__tests__/agents.test.d.ts.map +1 -0
- package/dist/__tests__/agents.test.js +673 -0
- package/dist/__tests__/agents.test.js.map +1 -0
- package/dist/__tests__/mcp-server.test.d.ts +2 -0
- package/dist/__tests__/mcp-server.test.d.ts.map +1 -0
- package/dist/__tests__/mcp-server.test.js +315 -0
- package/dist/__tests__/mcp-server.test.js.map +1 -0
- package/dist/__tests__/sequential-thinking.test.d.ts +2 -0
- package/dist/__tests__/sequential-thinking.test.d.ts.map +1 -0
- package/dist/__tests__/sequential-thinking.test.js +545 -0
- package/dist/__tests__/sequential-thinking.test.js.map +1 -0
- package/dist/__tests__/swarm-coordinator.test.d.ts +2 -0
- package/dist/__tests__/swarm-coordinator.test.d.ts.map +1 -0
- package/dist/__tests__/swarm-coordinator.test.js +606 -0
- package/dist/__tests__/swarm-coordinator.test.js.map +1 -0
- package/dist/__tests__/types.test.d.ts +2 -0
- package/dist/__tests__/types.test.d.ts.map +1 -0
- package/dist/__tests__/types.test.js +741 -0
- package/dist/__tests__/types.test.js.map +1 -0
- package/dist/__tests__/utils.test.d.ts +2 -0
- package/dist/__tests__/utils.test.d.ts.map +1 -0
- package/dist/__tests__/utils.test.js +264 -0
- package/dist/__tests__/utils.test.js.map +1 -0
- package/dist/agents/base-agent.d.ts +126 -0
- package/dist/agents/base-agent.d.ts.map +1 -0
- package/dist/agents/base-agent.js +214 -0
- package/dist/agents/base-agent.js.map +1 -0
- package/dist/agents/critic-agent.d.ts +134 -0
- package/dist/agents/critic-agent.d.ts.map +1 -0
- package/dist/agents/critic-agent.js +484 -0
- package/dist/agents/critic-agent.js.map +1 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +11 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/meta-reasoning-agent.d.ts +143 -0
- package/dist/agents/meta-reasoning-agent.d.ts.map +1 -0
- package/dist/agents/meta-reasoning-agent.js +532 -0
- package/dist/agents/meta-reasoning-agent.js.map +1 -0
- package/dist/agents/reasoner-agent.d.ts +75 -0
- package/dist/agents/reasoner-agent.d.ts.map +1 -0
- package/dist/agents/reasoner-agent.js +226 -0
- package/dist/agents/reasoner-agent.js.map +1 -0
- package/dist/agents/synthesizer-agent.d.ts +174 -0
- package/dist/agents/synthesizer-agent.d.ts.map +1 -0
- package/dist/agents/synthesizer-agent.js +583 -0
- package/dist/agents/synthesizer-agent.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp-server.d.ts +823 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +377 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/sequential-thinking.d.ts +91 -0
- package/dist/sequential-thinking.d.ts.map +1 -0
- package/dist/sequential-thinking.js +540 -0
- package/dist/sequential-thinking.js.map +1 -0
- package/dist/swarm-coordinator.d.ts +188 -0
- package/dist/swarm-coordinator.d.ts.map +1 -0
- package/dist/swarm-coordinator.js +627 -0
- package/dist/swarm-coordinator.js.map +1 -0
- package/dist/types/index.d.ts +806 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +279 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +421 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +864 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/llm-adapter.d.ts +23 -0
- package/dist/utils/llm-adapter.d.ts.map +1 -0
- package/dist/utils/llm-adapter.js +68 -0
- package/dist/utils/llm-adapter.js.map +1 -0
- package/dist/utils/persistence.d.ts +33 -0
- package/dist/utils/persistence.d.ts.map +1 -0
- package/dist/utils/persistence.js +108 -0
- package/dist/utils/persistence.js.map +1 -0
- package/package.json +41 -0
- package/src/__tests__/agents.test.ts +858 -0
- package/src/__tests__/mcp-server.test.ts +380 -0
- package/src/__tests__/sequential-thinking.test.ts +687 -0
- package/src/__tests__/swarm-coordinator.test.ts +903 -0
- package/src/__tests__/types.test.ts +839 -0
- package/src/__tests__/utils.test.ts +322 -0
- package/src/agents/base-agent.ts +285 -0
- package/src/agents/critic-agent.ts +582 -0
- package/src/agents/index.ts +11 -0
- package/src/agents/meta-reasoning-agent.ts +672 -0
- package/src/agents/reasoner-agent.ts +312 -0
- package/src/agents/synthesizer-agent.ts +758 -0
- package/src/index.ts +118 -0
- package/src/mcp-server.ts +387 -0
- package/src/sequential-thinking.ts +560 -0
- package/src/swarm-coordinator.ts +744 -0
- package/src/types/index.ts +915 -0
- package/src/utils/index.ts +1004 -0
- package/src/utils/llm-adapter.ts +76 -0
- package/src/utils/persistence.ts +108 -0
- package/test_output.log +0 -0
- package/tsconfig.json +21 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Sequential Thinking with Swarm Agents
|
|
3
|
+
* Main entry point for the library
|
|
4
|
+
* @module mcp-sequential-thinking
|
|
5
|
+
* @version 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Export MCP Server
|
|
9
|
+
export { MCPServer } from './mcp-server.js';
|
|
10
|
+
|
|
11
|
+
// Export Sequential Thinking components
|
|
12
|
+
export {
|
|
13
|
+
SessionManager,
|
|
14
|
+
ThoughtGraph,
|
|
15
|
+
MetaReasoningEngine,
|
|
16
|
+
SelfCorrectionEngine,
|
|
17
|
+
AdaptiveGranularityEngine,
|
|
18
|
+
ConfidenceCalibrator
|
|
19
|
+
} from './sequential-thinking.js';
|
|
20
|
+
|
|
21
|
+
// Export Swarm Coordinator components
|
|
22
|
+
export {
|
|
23
|
+
SwarmCoordinator,
|
|
24
|
+
AgentRegistry,
|
|
25
|
+
MessageBroker,
|
|
26
|
+
TaskQueueManager,
|
|
27
|
+
TaskAssigner,
|
|
28
|
+
ConsensusEngine,
|
|
29
|
+
ConflictResolver
|
|
30
|
+
} from './swarm-coordinator.js';
|
|
31
|
+
|
|
32
|
+
// Export Agents
|
|
33
|
+
export {
|
|
34
|
+
BaseAgent,
|
|
35
|
+
ReasonerAgent,
|
|
36
|
+
CriticAgent,
|
|
37
|
+
SynthesizerAgent,
|
|
38
|
+
MetaReasoningAgent
|
|
39
|
+
} from './agents/index.js';
|
|
40
|
+
|
|
41
|
+
// Export all types
|
|
42
|
+
export * from './types/index.js';
|
|
43
|
+
|
|
44
|
+
// Export utilities (avoiding Result conflict)
|
|
45
|
+
export {
|
|
46
|
+
generateId,
|
|
47
|
+
generateUUID,
|
|
48
|
+
generateShortId,
|
|
49
|
+
clamp,
|
|
50
|
+
clampConfidence,
|
|
51
|
+
lerp,
|
|
52
|
+
roundTo,
|
|
53
|
+
extractWords,
|
|
54
|
+
calculateJaccardSimilarity,
|
|
55
|
+
calculateSimilarity,
|
|
56
|
+
calculateCosineSimilarity,
|
|
57
|
+
extractNGrams,
|
|
58
|
+
calculateNGramSimilarity,
|
|
59
|
+
calculateMean,
|
|
60
|
+
calculateWeightedAverage,
|
|
61
|
+
calculateMedian,
|
|
62
|
+
calculateStandardDeviation,
|
|
63
|
+
calculateVariance,
|
|
64
|
+
findMin,
|
|
65
|
+
findMax,
|
|
66
|
+
normalizeValues,
|
|
67
|
+
calculatePercentile,
|
|
68
|
+
formatDuration,
|
|
69
|
+
formatTimestamp,
|
|
70
|
+
formatRelativeTime,
|
|
71
|
+
deepClone,
|
|
72
|
+
deepCloneWithDates,
|
|
73
|
+
deepMerge,
|
|
74
|
+
deepEqual,
|
|
75
|
+
pick,
|
|
76
|
+
omit,
|
|
77
|
+
validateUUID,
|
|
78
|
+
isNonEmptyString,
|
|
79
|
+
isPositiveNumber,
|
|
80
|
+
isInRange,
|
|
81
|
+
isNonEmptyArray,
|
|
82
|
+
isPlainObject,
|
|
83
|
+
sanitizeString,
|
|
84
|
+
truncateString,
|
|
85
|
+
toCamelCase,
|
|
86
|
+
toPascalCase,
|
|
87
|
+
toSnakeCase,
|
|
88
|
+
toKebabCase,
|
|
89
|
+
escapeRegExp,
|
|
90
|
+
unique,
|
|
91
|
+
uniqueBy,
|
|
92
|
+
groupBy,
|
|
93
|
+
partition,
|
|
94
|
+
shuffle,
|
|
95
|
+
sample,
|
|
96
|
+
chunk,
|
|
97
|
+
flatten,
|
|
98
|
+
deepFlatten,
|
|
99
|
+
sleep,
|
|
100
|
+
createTimeoutPromise,
|
|
101
|
+
withTimeout,
|
|
102
|
+
retry,
|
|
103
|
+
debounce,
|
|
104
|
+
throttle,
|
|
105
|
+
tryCatch,
|
|
106
|
+
tryCatchAsync,
|
|
107
|
+
createError
|
|
108
|
+
} from './utils/index.js';
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Library version
|
|
112
|
+
*/
|
|
113
|
+
export const VERSION = '1.0.0';
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Default export - MCPServer
|
|
117
|
+
*/
|
|
118
|
+
export { MCPServer as default } from './mcp-server.js';
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Server
|
|
3
|
+
*/
|
|
4
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
5
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
6
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import { SessionManager, SelfCorrectionEngine, AdaptiveGranularityEngine } from './sequential-thinking.js';
|
|
8
|
+
import { SwarmCoordinator } from './swarm-coordinator.js';
|
|
9
|
+
import { ThoughtType, AgentType, ReasoningStrategy, CriticType, SynthesizerType, ConsensusAlgorithm, ConflictResolutionStrategy } from './types/index.js';
|
|
10
|
+
import { ReasonerAgent, CriticAgent, SynthesizerAgent, MetaReasoningAgent } from './agents/index.js';
|
|
11
|
+
const SEQUENTIAL_THINKING_TOOL = {
|
|
12
|
+
name: 'advanced_sequential_thinking',
|
|
13
|
+
description: 'Advanced sequential thinking with self-correction, parallel hypotheses, meta-reasoning',
|
|
14
|
+
inputSchema: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
operation: {
|
|
18
|
+
type: 'string',
|
|
19
|
+
enum: [
|
|
20
|
+
'initialize', 'add_thought', 'revise_thought', 'branch_thought',
|
|
21
|
+
'merge_branches', 'prune_branch', 'self_correct', 'add_meta_thought',
|
|
22
|
+
'get_thinking_path', 'get_branch_comparison', 'calibrate_confidence',
|
|
23
|
+
'adjust_granularity', 'get_session_state', 'complete_session',
|
|
24
|
+
'delegate_to_swarm'
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
sessionId: { type: 'string' },
|
|
28
|
+
topic: { type: 'string' },
|
|
29
|
+
content: { type: 'string' },
|
|
30
|
+
thoughtType: { type: 'string', enum: Object.values(ThoughtType) },
|
|
31
|
+
thoughtId: { type: 'string' },
|
|
32
|
+
parentThoughtId: { type: 'string' },
|
|
33
|
+
branchId: { type: 'string' },
|
|
34
|
+
confidence: { type: 'number' },
|
|
35
|
+
revisionReason: { type: 'string' },
|
|
36
|
+
branchName: { type: 'string' },
|
|
37
|
+
branchDescription: { type: 'string' },
|
|
38
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
39
|
+
assumptions: { type: 'array', items: { type: 'string' } },
|
|
40
|
+
dependencies: { type: 'array', items: { type: 'string' } },
|
|
41
|
+
settings: { type: 'object' }
|
|
42
|
+
},
|
|
43
|
+
required: ['operation']
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const SWARM_COORDINATION_TOOL = {
|
|
47
|
+
name: 'swarm_agent_coordination',
|
|
48
|
+
description: 'Coordinate multiple AI agents in a swarm',
|
|
49
|
+
inputSchema: {
|
|
50
|
+
type: 'object',
|
|
51
|
+
properties: {
|
|
52
|
+
operation: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
enum: [
|
|
55
|
+
'register_agent', 'unregister_agent', 'list_agents', 'get_agent_status',
|
|
56
|
+
'assign_task', 'get_task_status', 'reach_consensus', 'identify_conflict',
|
|
57
|
+
'resolve_conflict', 'broadcast_message', 'send_direct_message', 'get_swarm_stats'
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
name: { type: 'string' },
|
|
61
|
+
agentType: { type: 'string', enum: Object.values(AgentType) },
|
|
62
|
+
subtype: { type: 'string' },
|
|
63
|
+
capabilities: { type: 'array' },
|
|
64
|
+
agentId: { type: 'string' },
|
|
65
|
+
taskType: { type: 'string' },
|
|
66
|
+
description: { type: 'string' },
|
|
67
|
+
input: {},
|
|
68
|
+
requiredCapabilities: { type: 'array', items: { type: 'string' } },
|
|
69
|
+
strategy: { type: 'string', enum: ['round_robin', 'capability_based', 'load_balanced', 'adaptive'] },
|
|
70
|
+
proposal: {},
|
|
71
|
+
options: { type: 'array' },
|
|
72
|
+
algorithm: { type: 'string', enum: Object.values(ConsensusAlgorithm) },
|
|
73
|
+
participatingAgents: { type: 'array', items: { type: 'string' } },
|
|
74
|
+
minAgreementRatio: { type: 'number' },
|
|
75
|
+
maxRounds: { type: 'number' },
|
|
76
|
+
conflictType: { type: 'string' },
|
|
77
|
+
positions: { type: 'array' },
|
|
78
|
+
conflictId: { type: 'string' },
|
|
79
|
+
resolutionStrategy: { type: 'string', enum: Object.values(ConflictResolutionStrategy) },
|
|
80
|
+
resolverAgentId: { type: 'string' },
|
|
81
|
+
senderId: { type: 'string' },
|
|
82
|
+
recipientId: { type: 'string' },
|
|
83
|
+
payload: {},
|
|
84
|
+
taskId: { type: 'string' }
|
|
85
|
+
},
|
|
86
|
+
required: ['operation']
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
export class MCPServer {
|
|
90
|
+
server;
|
|
91
|
+
sessionManager;
|
|
92
|
+
swarmCoordinator;
|
|
93
|
+
registeredAgents = new Map();
|
|
94
|
+
private dataDir = './data';
|
|
95
|
+
|
|
96
|
+
constructor() {
|
|
97
|
+
this.server = new Server({ name: 'mcp-sequential-thinking', version: '1.0.0' }, { capabilities: { tools: {} } });
|
|
98
|
+
this.sessionManager = new SessionManager(this.dataDir);
|
|
99
|
+
this.swarmCoordinator = new SwarmCoordinator(this.dataDir);
|
|
100
|
+
this.setupHandlers();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async initialize() {
|
|
104
|
+
await this.sessionManager.initialize();
|
|
105
|
+
await this.swarmCoordinator.initialize();
|
|
106
|
+
}
|
|
107
|
+
setupHandlers() {
|
|
108
|
+
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
109
|
+
tools: [SEQUENTIAL_THINKING_TOOL, SWARM_COORDINATION_TOOL]
|
|
110
|
+
}));
|
|
111
|
+
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
112
|
+
const { name, arguments: args } = request.params;
|
|
113
|
+
try {
|
|
114
|
+
if (name === 'advanced_sequential_thinking') {
|
|
115
|
+
const result = await this.handleSequentialThinking(args);
|
|
116
|
+
return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
|
|
117
|
+
}
|
|
118
|
+
if (name === 'swarm_agent_coordination') {
|
|
119
|
+
const result = await this.handleSwarmCoordination(args);
|
|
120
|
+
return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
return {
|
|
126
|
+
content: [{ type: 'text', text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : String(error) }, null, 2) }],
|
|
127
|
+
isError: true
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async handleSequentialThinking(args: any) {
|
|
133
|
+
const operation = String(args.operation);
|
|
134
|
+
switch (operation) {
|
|
135
|
+
case 'initialize': {
|
|
136
|
+
const session = await this.sessionManager.createSession(String(args.topic), args.settings || {});
|
|
137
|
+
return { success: true, sessionId: session.id, topic: session.topic, status: session.status, settings: session.settings };
|
|
138
|
+
}
|
|
139
|
+
case 'add_thought': {
|
|
140
|
+
const { thought, metaNote, issues } = await this.sessionManager.addThought(String(args.sessionId), String(args.content), {
|
|
141
|
+
thoughtType: args.thoughtType,
|
|
142
|
+
parentThoughtId: args.parentThoughtId,
|
|
143
|
+
branchId: args.branchId,
|
|
144
|
+
confidence: args.confidence,
|
|
145
|
+
tags: args.tags,
|
|
146
|
+
assumptions: args.assumptions,
|
|
147
|
+
dependencies: args.dependencies
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
success: true,
|
|
151
|
+
thought: { id: thought.id, stepNumber: thought.stepNumber, thoughtType: thought.thoughtType, confidence: thought.confidence, branchId: thought.branchId },
|
|
152
|
+
metaNote: metaNote ? { id: metaNote.id, type: metaNote.observationType, content: metaNote.content, triggeredActions: metaNote.triggeredActions } : undefined,
|
|
153
|
+
issuesDetected: issues
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
case 'revise_thought': {
|
|
157
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
158
|
+
const revision = graph.reviseThought(String(args.thoughtId), String(args.newContent), String(args.revisionReason));
|
|
159
|
+
return { success: true, revision: { id: revision.id, originalThoughtId: args.thoughtId, content: revision.content, revisionReason: args.revisionReason } };
|
|
160
|
+
}
|
|
161
|
+
case 'branch_thought': {
|
|
162
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
163
|
+
const { branch, thought } = graph.createBranch(String(args.sessionId), String(args.parentThoughtId), String(args.branchName), String(args.branchDescription), String(args.initialContent));
|
|
164
|
+
return { success: true, branch: { id: branch.id, name: branch.name, description: branch.description, parentBranchId: branch.parentBranchId }, rootThought: { id: thought.id, content: thought.content } };
|
|
165
|
+
}
|
|
166
|
+
case 'merge_branches': {
|
|
167
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
168
|
+
const mergeThought = graph.mergeBranches(String(args.targetBranchId), String(args.sourceBranchId), String(args.mergeContent));
|
|
169
|
+
return { success: true, mergeThought: { id: mergeThought.id, content: mergeThought.content, mergedBranches: [args.sourceBranchId] } };
|
|
170
|
+
}
|
|
171
|
+
case 'prune_branch': {
|
|
172
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
173
|
+
graph.pruneBranch(String(args.branchId), String(args.reason));
|
|
174
|
+
return { success: true, message: `Branch ${args.branchId} pruned: ${args.reason}` };
|
|
175
|
+
}
|
|
176
|
+
case 'self_correct': {
|
|
177
|
+
const session = this.sessionManager.getSession(String(args.sessionId));
|
|
178
|
+
if (!session)
|
|
179
|
+
throw new Error(`Session not found: ${args.sessionId}`);
|
|
180
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
181
|
+
const thought = graph.getThoughts().get(String(args.thoughtId));
|
|
182
|
+
if (!thought)
|
|
183
|
+
throw new Error(`Thought not found: ${args.thoughtId}`);
|
|
184
|
+
const correctionEngine = new SelfCorrectionEngine();
|
|
185
|
+
const issues = correctionEngine.detectIssues(thought, graph);
|
|
186
|
+
return { success: true, issuesDetected: issues, recommendations: issues.map((i: any) => i.suggestion) };
|
|
187
|
+
}
|
|
188
|
+
case 'add_meta_thought': {
|
|
189
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
190
|
+
const metaEngine = this.sessionManager.getMetaEngine();
|
|
191
|
+
const metaNote = metaEngine.generateMetaObservation(String(args.thoughtId), graph);
|
|
192
|
+
return { success: true, metaNote: metaNote ? { id: metaNote.id, type: metaNote.observationType, content: metaNote.content, triggeredActions: metaNote.triggeredActions } : null };
|
|
193
|
+
}
|
|
194
|
+
case 'get_thinking_path': {
|
|
195
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
196
|
+
const path = graph.getThinkingPath(String(args.thoughtId));
|
|
197
|
+
return { success: true, path: path.map((t: any) => ({ id: t.id, stepNumber: t.stepNumber, thoughtType: t.thoughtType, content: t.content.substring(0, 200), confidence: t.confidence.overall })) };
|
|
198
|
+
}
|
|
199
|
+
case 'get_branch_comparison': {
|
|
200
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
201
|
+
const branchIds = Array.isArray(args.branchIds) ? args.branchIds : [];
|
|
202
|
+
const comparison = graph.getBranchComparison(branchIds);
|
|
203
|
+
return { success: true, comparison };
|
|
204
|
+
}
|
|
205
|
+
case 'calibrate_confidence': {
|
|
206
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
207
|
+
const thought = graph.getThoughts().get(String(args.thoughtId));
|
|
208
|
+
if (!thought)
|
|
209
|
+
throw new Error(`Thought not found: ${args.thoughtId}`);
|
|
210
|
+
return { success: true, originalConfidence: thought.confidence, message: 'Confidence calibration would update based on feedback' };
|
|
211
|
+
}
|
|
212
|
+
case 'adjust_granularity': {
|
|
213
|
+
const session = this.sessionManager.getSession(String(args.sessionId));
|
|
214
|
+
if (!session)
|
|
215
|
+
throw new Error(`Session not found: ${args.sessionId}`);
|
|
216
|
+
const engine = new AdaptiveGranularityEngine();
|
|
217
|
+
const granularity = engine.calculateGranularity(session.topic, session.context, session.thoughts.size);
|
|
218
|
+
return { success: true, recommendedDetail: granularity.detail, reasoning: granularity.reasoning };
|
|
219
|
+
}
|
|
220
|
+
case 'get_session_state': {
|
|
221
|
+
const session = this.sessionManager.getSession(String(args.sessionId));
|
|
222
|
+
if (!session)
|
|
223
|
+
throw new Error(`Session not found: ${args.sessionId}`);
|
|
224
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
225
|
+
return {
|
|
226
|
+
success: true,
|
|
227
|
+
session: { id: session.id, topic: session.topic, status: session.status, totalThoughts: session.metadata.totalThoughts, totalBranches: session.metadata.totalBranches, lastActivity: session.metadata.lastActivityAt },
|
|
228
|
+
branches: Array.from(graph.getBranches().values()).map(b => ({ id: b.id, name: b.name, status: b.status, thoughtCount: b.thoughtIds.length, confidence: b.confidence })),
|
|
229
|
+
recentThoughts: Array.from(session.thoughts.values()).slice(-5).map((t: any) => ({ id: t.id, stepNumber: t.stepNumber, thoughtType: t.thoughtType, content: t.content.substring(0, 100) }))
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
case 'complete_session': {
|
|
233
|
+
await this.sessionManager.completeSession(String(args.sessionId));
|
|
234
|
+
return { success: true, message: `Session ${args.sessionId} completed` };
|
|
235
|
+
}
|
|
236
|
+
case 'delegate_to_swarm': {
|
|
237
|
+
const session = this.sessionManager.getSession(String(args.sessionId));
|
|
238
|
+
if (!session) throw new Error(`Session not found: ${args.sessionId}`);
|
|
239
|
+
|
|
240
|
+
const graph = this.sessionManager.getThoughtGraph();
|
|
241
|
+
const thought = graph.getThoughts().get(String(args.thoughtId));
|
|
242
|
+
if (!thought) throw new Error(`Thought not found: ${args.thoughtId}`);
|
|
243
|
+
|
|
244
|
+
const taskType = String(args.taskType || 'reasoning');
|
|
245
|
+
const { task, assignedAgent } = this.swarmCoordinator.submitTask(
|
|
246
|
+
taskType,
|
|
247
|
+
`Analyze and expand on this thought: ${thought.content}`,
|
|
248
|
+
{ thought: thought.content, context: session.context },
|
|
249
|
+
{ requiredCapabilities: args.requiredCapabilities || [], minConfidence: 0.6, maxTimeMs: 30000 }
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
if (!assignedAgent) {
|
|
253
|
+
return { success: false, error: 'No suitable agent available in swarm' };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const result = await this.swarmCoordinator.executeTask(task.id);
|
|
257
|
+
|
|
258
|
+
if (result.success && result.output) {
|
|
259
|
+
const swarmOutput = typeof result.output === 'string' ? result.output : JSON.stringify(result.output, null, 2);
|
|
260
|
+
const newThought = await this.sessionManager.addThought(session.id, swarmOutput, {
|
|
261
|
+
thoughtType: ThoughtType.SYNTHESIS,
|
|
262
|
+
parentThoughtId: thought.id,
|
|
263
|
+
branchId: thought.branchId,
|
|
264
|
+
confidence: result.confidence,
|
|
265
|
+
tags: ['swarm-result', assignedAgent.config.name]
|
|
266
|
+
});
|
|
267
|
+
return { success: true, swarmResult: result, newThought: newThought.thought };
|
|
268
|
+
} else {
|
|
269
|
+
return { success: false, error: 'Swarm execution failed', details: result };
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
default:
|
|
273
|
+
throw new Error(`Unknown operation: ${operation}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async handleSwarmCoordination(args: any) {
|
|
277
|
+
const operation = String(args.operation);
|
|
278
|
+
switch (operation) {
|
|
279
|
+
case 'register_agent': {
|
|
280
|
+
const agentType = String(args.agentType);
|
|
281
|
+
let agent;
|
|
282
|
+
switch (agentType) {
|
|
283
|
+
case AgentType.REASONER:
|
|
284
|
+
agent = new ReasonerAgent({ name: String(args.name), strategy: args.subtype || ReasoningStrategy.CHAIN_OF_THOUGHT });
|
|
285
|
+
break;
|
|
286
|
+
case AgentType.CRITIC:
|
|
287
|
+
agent = new CriticAgent({ name: String(args.name), criticType: args.subtype || CriticType.LOGICAL });
|
|
288
|
+
break;
|
|
289
|
+
case AgentType.SYNTHESIZER:
|
|
290
|
+
agent = new SynthesizerAgent({ name: String(args.name), synthesizerType: args.subtype || SynthesizerType.CONSENSUS });
|
|
291
|
+
break;
|
|
292
|
+
case AgentType.META_REASONING:
|
|
293
|
+
agent = new MetaReasoningAgent({ name: String(args.name) });
|
|
294
|
+
break;
|
|
295
|
+
default:
|
|
296
|
+
throw new Error(`Unsupported agent type: ${agentType}`);
|
|
297
|
+
}
|
|
298
|
+
this.registeredAgents.set(agent.id, agent);
|
|
299
|
+
await this.swarmCoordinator.registerAgent(agent);
|
|
300
|
+
return { success: true, agentId: agent.id, name: agent.config.name, type: agent.config.type, capabilities: agent.config.capabilities.map(c => c.name) };
|
|
301
|
+
}
|
|
302
|
+
case 'unregister_agent': {
|
|
303
|
+
const success = await this.swarmCoordinator.unregisterAgent(String(args.agentId));
|
|
304
|
+
this.registeredAgents.delete(String(args.agentId));
|
|
305
|
+
return { success, message: success ? `Agent ${args.agentId} unregistered` : 'Agent not found' };
|
|
306
|
+
}
|
|
307
|
+
case 'list_agents': {
|
|
308
|
+
const agents = this.swarmCoordinator.registry.getAllAgents();
|
|
309
|
+
return { success: true, agents: agents.map(a => ({ id: a.config.id, name: a.config.name, type: a.config.type, subtype: a.config.subtype, status: a.status, capabilities: a.config.capabilities.map(c => c.name) })) };
|
|
310
|
+
}
|
|
311
|
+
case 'get_agent_status': {
|
|
312
|
+
const agent = this.swarmCoordinator.registry.getAgent(String(args.agentId));
|
|
313
|
+
if (!agent)
|
|
314
|
+
throw new Error(`Agent not found: ${args.agentId}`);
|
|
315
|
+
return { success: true, agent: { id: agent.config.id, name: agent.config.name, type: agent.config.type, status: agent.status, currentTasks: agent.currentTasks, performanceMetrics: { tasksCompleted: agent.performanceHistory.length, averageQuality: agent.performanceHistory.length > 0 ? agent.performanceHistory.reduce((sum, p) => sum + p.quality, 0) / agent.performanceHistory.length : 0, averageTimeMs: agent.performanceHistory.length > 0 ? agent.performanceHistory.reduce((sum, p) => sum + p.timeMs, 0) / agent.performanceHistory.length : 0 } } };
|
|
316
|
+
}
|
|
317
|
+
case 'assign_task': {
|
|
318
|
+
const { task, assignedAgent } = this.swarmCoordinator.submitTask(String(args.taskType), String(args.description), args.input, { requiredCapabilities: Array.isArray(args.requiredCapabilities) ? args.requiredCapabilities : [], minConfidence: 0.6, maxTimeMs: 30000 }, args.strategy || 'adaptive');
|
|
319
|
+
|
|
320
|
+
let result = null;
|
|
321
|
+
if (assignedAgent) {
|
|
322
|
+
result = await this.swarmCoordinator.executeTask(task.id);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
success: true,
|
|
327
|
+
task: {
|
|
328
|
+
id: task.id,
|
|
329
|
+
type: task.type,
|
|
330
|
+
description: task.description,
|
|
331
|
+
status: task.status,
|
|
332
|
+
assignedAgentId: task.assignedAgentId
|
|
333
|
+
},
|
|
334
|
+
assignedAgent: assignedAgent ? {
|
|
335
|
+
id: assignedAgent.config.id,
|
|
336
|
+
name: assignedAgent.config.name,
|
|
337
|
+
type: assignedAgent.config.type
|
|
338
|
+
} : null,
|
|
339
|
+
result
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
case 'get_task_status': {
|
|
343
|
+
const task = this.swarmCoordinator.taskQueue.getTask(String(args.taskId));
|
|
344
|
+
if (!task)
|
|
345
|
+
throw new Error(`Task not found: ${args.taskId}`);
|
|
346
|
+
return { success: true, task: { id: task.id, type: task.type, description: task.description, status: task.status, assignedAgentId: task.assignedAgentId, result: task.result, createdAt: task.metadata.createdAt, completedAt: task.metadata.completedAt } };
|
|
347
|
+
}
|
|
348
|
+
case 'reach_consensus': {
|
|
349
|
+
const result = await this.swarmCoordinator.reachConsensus(args.proposal, Array.isArray(args.options) ? args.options : [], args.algorithm || ConsensusAlgorithm.WEIGHTED_VOTE, Array.isArray(args.participatingAgents) ? args.participatingAgents : undefined, typeof args.minAgreementRatio === 'number' ? args.minAgreementRatio : 0.67, typeof args.maxRounds === 'number' ? args.maxRounds : 3);
|
|
350
|
+
return { success: true, consensus: { proposalId: result.proposalId, algorithm: result.algorithm, winningOption: result.winningOption, confidence: result.confidence, agreementRatio: result.agreementRatio, rounds: result.metadata.rounds, dissentingOpinions: result.dissentingOpinions } };
|
|
351
|
+
}
|
|
352
|
+
case 'identify_conflict': {
|
|
353
|
+
const conflict = this.swarmCoordinator.identifyConflict(String(args.conflictType), String(args.description), Array.isArray(args.positions) ? args.positions : []);
|
|
354
|
+
return { success: true, conflict: { id: conflict.id, type: conflict.type, description: conflict.description, involvedAgents: conflict.involvedAgents, positions: conflict.conflictingPositions, status: conflict.status } };
|
|
355
|
+
}
|
|
356
|
+
case 'resolve_conflict': {
|
|
357
|
+
const { conflict, resolution } = this.swarmCoordinator.resolveConflict(String(args.conflictId), args.resolutionStrategy || ConflictResolutionStrategy.VOTING, args.resolverAgentId);
|
|
358
|
+
return { success: true, conflict: { id: conflict.id, status: conflict.status, resolutionStrategy: conflict.metadata.resolutionStrategy }, resolution };
|
|
359
|
+
}
|
|
360
|
+
case 'broadcast_message': {
|
|
361
|
+
const message = this.swarmCoordinator.broadcastMessage(String(args.senderId), args.payload);
|
|
362
|
+
return { success: true, message: { id: message.id, type: message.type, senderId: message.senderId, timestamp: message.timestamp } };
|
|
363
|
+
}
|
|
364
|
+
case 'send_direct_message': {
|
|
365
|
+
const message = this.swarmCoordinator.sendDirectMessage(String(args.senderId), String(args.recipientId), args.payload);
|
|
366
|
+
return { success: true, message: { id: message.id, type: message.type, senderId: message.senderId, recipientId: message.recipientId, timestamp: message.timestamp } };
|
|
367
|
+
}
|
|
368
|
+
case 'get_swarm_stats': {
|
|
369
|
+
const stats = this.swarmCoordinator.getSwarmStats();
|
|
370
|
+
return { success: true, stats };
|
|
371
|
+
}
|
|
372
|
+
default:
|
|
373
|
+
throw new Error(`Unknown operation: ${operation}`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
async start() {
|
|
377
|
+
const transport = new StdioServerTransport();
|
|
378
|
+
await this.server.connect(transport);
|
|
379
|
+
console.error('MCP Sequential Thinking Server running on stdio');
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const isMainModule = import.meta.url === `file://${process.argv[1]}`;
|
|
383
|
+
if (isMainModule) {
|
|
384
|
+
const server = new MCPServer();
|
|
385
|
+
server.start().catch(console.error);
|
|
386
|
+
}
|
|
387
|
+
//# sourceMappingURL=mcp-server.js.map
|