mirror of
https://github.com/modelcontextprotocol/servers.git
synced 2026-02-19 11:54:58 -05:00
Add tests for sequential thinking and update typescript sdk versions
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -298,3 +298,4 @@ cython_debug/
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
.claude/settings.local.json
|
||||
|
||||
1047
package-lock.json
generated
1047
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -22,7 +22,7 @@
|
||||
"start:streamableHttp": "node dist/streamableHttp.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.12.0",
|
||||
"@modelcontextprotocol/sdk": "^1.19.1",
|
||||
"express": "^4.21.1",
|
||||
"zod": "^3.23.8",
|
||||
"zod-to-json-schema": "^3.23.5"
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
"test": "jest --config=jest.config.cjs --coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.17.0",
|
||||
"@modelcontextprotocol/sdk": "^1.19.1",
|
||||
"diff": "^5.1.0",
|
||||
"glob": "^10.3.10",
|
||||
"minimatch": "^10.0.1",
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
"watch": "tsc --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "1.0.1"
|
||||
"@modelcontextprotocol/sdk": "^1.19.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22",
|
||||
|
||||
440
src/sequentialthinking/__tests__/lib.test.ts
Normal file
440
src/sequentialthinking/__tests__/lib.test.ts
Normal file
@@ -0,0 +1,440 @@
|
||||
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
|
||||
import { SequentialThinkingServer, ThoughtData } from '../lib.js';
|
||||
|
||||
// Mock chalk to avoid ESM issues in Jest
|
||||
jest.mock('chalk', () => {
|
||||
const chalkMock = {
|
||||
yellow: (str: string) => str,
|
||||
green: (str: string) => str,
|
||||
blue: (str: string) => str,
|
||||
};
|
||||
return {
|
||||
default: chalkMock,
|
||||
__esModule: true,
|
||||
};
|
||||
});
|
||||
|
||||
describe('SequentialThinkingServer', () => {
|
||||
let server: SequentialThinkingServer;
|
||||
|
||||
beforeEach(() => {
|
||||
// Disable thought logging for tests
|
||||
process.env.DISABLE_THOUGHT_LOGGING = 'true';
|
||||
server = new SequentialThinkingServer();
|
||||
});
|
||||
|
||||
describe('processThought - validation', () => {
|
||||
it('should reject input with missing thought', () => {
|
||||
const input = {
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid thought');
|
||||
});
|
||||
|
||||
it('should reject input with non-string thought', () => {
|
||||
const input = {
|
||||
thought: 123,
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid thought');
|
||||
});
|
||||
|
||||
it('should reject input with missing thoughtNumber', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid thoughtNumber');
|
||||
});
|
||||
|
||||
it('should reject input with non-number thoughtNumber', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: '1',
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid thoughtNumber');
|
||||
});
|
||||
|
||||
it('should reject input with missing totalThoughts', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: 1,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid totalThoughts');
|
||||
});
|
||||
|
||||
it('should reject input with non-number totalThoughts', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: '3',
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid totalThoughts');
|
||||
});
|
||||
|
||||
it('should reject input with missing nextThoughtNeeded', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid nextThoughtNeeded');
|
||||
});
|
||||
|
||||
it('should reject input with non-boolean nextThoughtNeeded', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: 'true'
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid nextThoughtNeeded');
|
||||
});
|
||||
});
|
||||
|
||||
describe('processThought - valid inputs', () => {
|
||||
it('should accept valid basic thought', () => {
|
||||
const input = {
|
||||
thought: 'This is my first thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
expect(data.thoughtNumber).toBe(1);
|
||||
expect(data.totalThoughts).toBe(3);
|
||||
expect(data.nextThoughtNeeded).toBe(true);
|
||||
expect(data.thoughtHistoryLength).toBe(1);
|
||||
});
|
||||
|
||||
it('should accept thought with optional fields', () => {
|
||||
const input = {
|
||||
thought: 'Revising my earlier idea',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true,
|
||||
isRevision: true,
|
||||
revisesThought: 1,
|
||||
needsMoreThoughts: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
expect(data.thoughtNumber).toBe(2);
|
||||
expect(data.thoughtHistoryLength).toBe(1);
|
||||
});
|
||||
|
||||
it('should track multiple thoughts in history', () => {
|
||||
const input1 = {
|
||||
thought: 'First thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const input2 = {
|
||||
thought: 'Second thought',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const input3 = {
|
||||
thought: 'Final thought',
|
||||
thoughtNumber: 3,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
server.processThought(input1);
|
||||
server.processThought(input2);
|
||||
const result = server.processThought(input3);
|
||||
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
expect(data.thoughtHistoryLength).toBe(3);
|
||||
expect(data.nextThoughtNeeded).toBe(false);
|
||||
});
|
||||
|
||||
it('should auto-adjust totalThoughts if thoughtNumber exceeds it', () => {
|
||||
const input = {
|
||||
thought: 'Thought 5',
|
||||
thoughtNumber: 5,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
|
||||
expect(data.totalThoughts).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('processThought - branching', () => {
|
||||
it('should track branches correctly', () => {
|
||||
const input1 = {
|
||||
thought: 'Main thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const input2 = {
|
||||
thought: 'Branch A thought',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true,
|
||||
branchFromThought: 1,
|
||||
branchId: 'branch-a'
|
||||
};
|
||||
|
||||
const input3 = {
|
||||
thought: 'Branch B thought',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: false,
|
||||
branchFromThought: 1,
|
||||
branchId: 'branch-b'
|
||||
};
|
||||
|
||||
server.processThought(input1);
|
||||
server.processThought(input2);
|
||||
const result = server.processThought(input3);
|
||||
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
expect(data.branches).toContain('branch-a');
|
||||
expect(data.branches).toContain('branch-b');
|
||||
expect(data.branches.length).toBe(2);
|
||||
expect(data.thoughtHistoryLength).toBe(3);
|
||||
});
|
||||
|
||||
it('should allow multiple thoughts in same branch', () => {
|
||||
const input1 = {
|
||||
thought: 'Branch thought 1',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 2,
|
||||
nextThoughtNeeded: true,
|
||||
branchFromThought: 1,
|
||||
branchId: 'branch-a'
|
||||
};
|
||||
|
||||
const input2 = {
|
||||
thought: 'Branch thought 2',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 2,
|
||||
nextThoughtNeeded: false,
|
||||
branchFromThought: 1,
|
||||
branchId: 'branch-a'
|
||||
};
|
||||
|
||||
server.processThought(input1);
|
||||
const result = server.processThought(input2);
|
||||
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
expect(data.branches).toContain('branch-a');
|
||||
expect(data.branches.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('processThought - edge cases', () => {
|
||||
it('should reject empty thought string', () => {
|
||||
const input = {
|
||||
thought: '',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 1,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content[0].text).toContain('Invalid thought');
|
||||
});
|
||||
|
||||
it('should handle very long thought strings', () => {
|
||||
const input = {
|
||||
thought: 'a'.repeat(10000),
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 1,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle thoughtNumber = 1, totalThoughts = 1', () => {
|
||||
const input = {
|
||||
thought: 'Only thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 1,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
expect(data.thoughtNumber).toBe(1);
|
||||
expect(data.totalThoughts).toBe(1);
|
||||
});
|
||||
|
||||
it('should handle nextThoughtNeeded = false', () => {
|
||||
const input = {
|
||||
thought: 'Final thought',
|
||||
thoughtNumber: 3,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
const data = JSON.parse(result.content[0].text);
|
||||
|
||||
expect(data.nextThoughtNeeded).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('processThought - response format', () => {
|
||||
it('should return correct response structure on success', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 1,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
|
||||
expect(result).toHaveProperty('content');
|
||||
expect(Array.isArray(result.content)).toBe(true);
|
||||
expect(result.content.length).toBe(1);
|
||||
expect(result.content[0]).toHaveProperty('type', 'text');
|
||||
expect(result.content[0]).toHaveProperty('text');
|
||||
});
|
||||
|
||||
it('should return correct error structure on failure', () => {
|
||||
const input = {
|
||||
thought: 'Test',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 1
|
||||
// missing nextThoughtNeeded
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
|
||||
expect(result).toHaveProperty('isError', true);
|
||||
expect(result).toHaveProperty('content');
|
||||
expect(Array.isArray(result.content)).toBe(true);
|
||||
|
||||
const errorData = JSON.parse(result.content[0].text);
|
||||
expect(errorData).toHaveProperty('error');
|
||||
expect(errorData).toHaveProperty('status', 'failed');
|
||||
});
|
||||
|
||||
it('should return valid JSON in response', () => {
|
||||
const input = {
|
||||
thought: 'Test thought',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 1,
|
||||
nextThoughtNeeded: false
|
||||
};
|
||||
|
||||
const result = server.processThought(input);
|
||||
|
||||
expect(() => JSON.parse(result.content[0].text)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('processThought - with logging enabled', () => {
|
||||
let serverWithLogging: SequentialThinkingServer;
|
||||
|
||||
beforeEach(() => {
|
||||
// Enable thought logging for these tests
|
||||
delete process.env.DISABLE_THOUGHT_LOGGING;
|
||||
serverWithLogging = new SequentialThinkingServer();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Reset to disabled for other tests
|
||||
process.env.DISABLE_THOUGHT_LOGGING = 'true';
|
||||
});
|
||||
|
||||
it('should format and log regular thoughts', () => {
|
||||
const input = {
|
||||
thought: 'Test thought with logging',
|
||||
thoughtNumber: 1,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true
|
||||
};
|
||||
|
||||
const result = serverWithLogging.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should format and log revision thoughts', () => {
|
||||
const input = {
|
||||
thought: 'Revised thought',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: true,
|
||||
isRevision: true,
|
||||
revisesThought: 1
|
||||
};
|
||||
|
||||
const result = serverWithLogging.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should format and log branch thoughts', () => {
|
||||
const input = {
|
||||
thought: 'Branch thought',
|
||||
thoughtNumber: 2,
|
||||
totalThoughts: 3,
|
||||
nextThoughtNeeded: false,
|
||||
branchFromThought: 1,
|
||||
branchId: 'branch-a'
|
||||
};
|
||||
|
||||
const result = serverWithLogging.processThought(input);
|
||||
expect(result.isError).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,135 +7,7 @@ import {
|
||||
ListToolsRequestSchema,
|
||||
Tool,
|
||||
} from "@modelcontextprotocol/sdk/types.js";
|
||||
// Fixed chalk import for ESM
|
||||
import chalk from 'chalk';
|
||||
|
||||
interface ThoughtData {
|
||||
thought: string;
|
||||
thoughtNumber: number;
|
||||
totalThoughts: number;
|
||||
isRevision?: boolean;
|
||||
revisesThought?: number;
|
||||
branchFromThought?: number;
|
||||
branchId?: string;
|
||||
needsMoreThoughts?: boolean;
|
||||
nextThoughtNeeded: boolean;
|
||||
}
|
||||
|
||||
class SequentialThinkingServer {
|
||||
private thoughtHistory: ThoughtData[] = [];
|
||||
private branches: Record<string, ThoughtData[]> = {};
|
||||
private disableThoughtLogging: boolean;
|
||||
|
||||
constructor() {
|
||||
this.disableThoughtLogging = (process.env.DISABLE_THOUGHT_LOGGING || "").toLowerCase() === "true";
|
||||
}
|
||||
|
||||
private validateThoughtData(input: unknown): ThoughtData {
|
||||
const data = input as Record<string, unknown>;
|
||||
|
||||
if (!data.thought || typeof data.thought !== 'string') {
|
||||
throw new Error('Invalid thought: must be a string');
|
||||
}
|
||||
if (!data.thoughtNumber || typeof data.thoughtNumber !== 'number') {
|
||||
throw new Error('Invalid thoughtNumber: must be a number');
|
||||
}
|
||||
if (!data.totalThoughts || typeof data.totalThoughts !== 'number') {
|
||||
throw new Error('Invalid totalThoughts: must be a number');
|
||||
}
|
||||
if (typeof data.nextThoughtNeeded !== 'boolean') {
|
||||
throw new Error('Invalid nextThoughtNeeded: must be a boolean');
|
||||
}
|
||||
|
||||
return {
|
||||
thought: data.thought,
|
||||
thoughtNumber: data.thoughtNumber,
|
||||
totalThoughts: data.totalThoughts,
|
||||
nextThoughtNeeded: data.nextThoughtNeeded,
|
||||
isRevision: data.isRevision as boolean | undefined,
|
||||
revisesThought: data.revisesThought as number | undefined,
|
||||
branchFromThought: data.branchFromThought as number | undefined,
|
||||
branchId: data.branchId as string | undefined,
|
||||
needsMoreThoughts: data.needsMoreThoughts as boolean | undefined,
|
||||
};
|
||||
}
|
||||
|
||||
private formatThought(thoughtData: ThoughtData): string {
|
||||
const { thoughtNumber, totalThoughts, thought, isRevision, revisesThought, branchFromThought, branchId } = thoughtData;
|
||||
|
||||
let prefix = '';
|
||||
let context = '';
|
||||
|
||||
if (isRevision) {
|
||||
prefix = chalk.yellow('🔄 Revision');
|
||||
context = ` (revising thought ${revisesThought})`;
|
||||
} else if (branchFromThought) {
|
||||
prefix = chalk.green('🌿 Branch');
|
||||
context = ` (from thought ${branchFromThought}, ID: ${branchId})`;
|
||||
} else {
|
||||
prefix = chalk.blue('💭 Thought');
|
||||
context = '';
|
||||
}
|
||||
|
||||
const header = `${prefix} ${thoughtNumber}/${totalThoughts}${context}`;
|
||||
const border = '─'.repeat(Math.max(header.length, thought.length) + 4);
|
||||
|
||||
return `
|
||||
┌${border}┐
|
||||
│ ${header} │
|
||||
├${border}┤
|
||||
│ ${thought.padEnd(border.length - 2)} │
|
||||
└${border}┘`;
|
||||
}
|
||||
|
||||
public processThought(input: unknown): { content: Array<{ type: string; text: string }>; isError?: boolean } {
|
||||
try {
|
||||
const validatedInput = this.validateThoughtData(input);
|
||||
|
||||
if (validatedInput.thoughtNumber > validatedInput.totalThoughts) {
|
||||
validatedInput.totalThoughts = validatedInput.thoughtNumber;
|
||||
}
|
||||
|
||||
this.thoughtHistory.push(validatedInput);
|
||||
|
||||
if (validatedInput.branchFromThought && validatedInput.branchId) {
|
||||
if (!this.branches[validatedInput.branchId]) {
|
||||
this.branches[validatedInput.branchId] = [];
|
||||
}
|
||||
this.branches[validatedInput.branchId].push(validatedInput);
|
||||
}
|
||||
|
||||
if (!this.disableThoughtLogging) {
|
||||
const formattedThought = this.formatThought(validatedInput);
|
||||
console.error(formattedThought);
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: JSON.stringify({
|
||||
thoughtNumber: validatedInput.thoughtNumber,
|
||||
totalThoughts: validatedInput.totalThoughts,
|
||||
nextThoughtNeeded: validatedInput.nextThoughtNeeded,
|
||||
branches: Object.keys(this.branches),
|
||||
thoughtHistoryLength: this.thoughtHistory.length
|
||||
}, null, 2)
|
||||
}]
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: JSON.stringify({
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
status: 'failed'
|
||||
}, null, 2)
|
||||
}],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
import { SequentialThinkingServer } from './lib.js';
|
||||
|
||||
const SEQUENTIAL_THINKING_TOOL: Tool = {
|
||||
name: "sequentialthinking",
|
||||
|
||||
26
src/sequentialthinking/jest.config.cjs
Normal file
26
src/sequentialthinking/jest.config.cjs
Normal file
@@ -0,0 +1,26 @@
|
||||
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
extensionsToTreatAsEsm: ['.ts'],
|
||||
moduleNameMapper: {
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
transform: {
|
||||
'^.+\\.tsx?$': [
|
||||
'ts-jest',
|
||||
{
|
||||
useESM: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
transformIgnorePatterns: [
|
||||
'node_modules/(?!(chalk)/)',
|
||||
],
|
||||
testMatch: ['**/__tests__/**/*.test.ts'],
|
||||
collectCoverageFrom: [
|
||||
'**/*.ts',
|
||||
'!**/__tests__/**',
|
||||
'!**/dist/**',
|
||||
],
|
||||
}
|
||||
128
src/sequentialthinking/lib.ts
Normal file
128
src/sequentialthinking/lib.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import chalk from 'chalk';
|
||||
|
||||
export interface ThoughtData {
|
||||
thought: string;
|
||||
thoughtNumber: number;
|
||||
totalThoughts: number;
|
||||
isRevision?: boolean;
|
||||
revisesThought?: number;
|
||||
branchFromThought?: number;
|
||||
branchId?: string;
|
||||
needsMoreThoughts?: boolean;
|
||||
nextThoughtNeeded: boolean;
|
||||
}
|
||||
|
||||
export class SequentialThinkingServer {
|
||||
private thoughtHistory: ThoughtData[] = [];
|
||||
private branches: Record<string, ThoughtData[]> = {};
|
||||
private disableThoughtLogging: boolean;
|
||||
|
||||
constructor() {
|
||||
this.disableThoughtLogging = (process.env.DISABLE_THOUGHT_LOGGING || "").toLowerCase() === "true";
|
||||
}
|
||||
|
||||
private validateThoughtData(input: unknown): ThoughtData {
|
||||
const data = input as Record<string, unknown>;
|
||||
|
||||
if (!data.thought || typeof data.thought !== 'string') {
|
||||
throw new Error('Invalid thought: must be a string');
|
||||
}
|
||||
if (!data.thoughtNumber || typeof data.thoughtNumber !== 'number') {
|
||||
throw new Error('Invalid thoughtNumber: must be a number');
|
||||
}
|
||||
if (!data.totalThoughts || typeof data.totalThoughts !== 'number') {
|
||||
throw new Error('Invalid totalThoughts: must be a number');
|
||||
}
|
||||
if (typeof data.nextThoughtNeeded !== 'boolean') {
|
||||
throw new Error('Invalid nextThoughtNeeded: must be a boolean');
|
||||
}
|
||||
|
||||
return {
|
||||
thought: data.thought,
|
||||
thoughtNumber: data.thoughtNumber,
|
||||
totalThoughts: data.totalThoughts,
|
||||
nextThoughtNeeded: data.nextThoughtNeeded,
|
||||
isRevision: data.isRevision as boolean | undefined,
|
||||
revisesThought: data.revisesThought as number | undefined,
|
||||
branchFromThought: data.branchFromThought as number | undefined,
|
||||
branchId: data.branchId as string | undefined,
|
||||
needsMoreThoughts: data.needsMoreThoughts as boolean | undefined,
|
||||
};
|
||||
}
|
||||
|
||||
private formatThought(thoughtData: ThoughtData): string {
|
||||
const { thoughtNumber, totalThoughts, thought, isRevision, revisesThought, branchFromThought, branchId } = thoughtData;
|
||||
|
||||
let prefix = '';
|
||||
let context = '';
|
||||
|
||||
if (isRevision) {
|
||||
prefix = chalk.yellow('🔄 Revision');
|
||||
context = ` (revising thought ${revisesThought})`;
|
||||
} else if (branchFromThought) {
|
||||
prefix = chalk.green('🌿 Branch');
|
||||
context = ` (from thought ${branchFromThought}, ID: ${branchId})`;
|
||||
} else {
|
||||
prefix = chalk.blue('💭 Thought');
|
||||
context = '';
|
||||
}
|
||||
|
||||
const header = `${prefix} ${thoughtNumber}/${totalThoughts}${context}`;
|
||||
const border = '─'.repeat(Math.max(header.length, thought.length) + 4);
|
||||
|
||||
return `
|
||||
┌${border}┐
|
||||
│ ${header} │
|
||||
├${border}┤
|
||||
│ ${thought.padEnd(border.length - 2)} │
|
||||
└${border}┘`;
|
||||
}
|
||||
|
||||
public processThought(input: unknown): { content: Array<{ type: string; text: string }>; isError?: boolean } {
|
||||
try {
|
||||
const validatedInput = this.validateThoughtData(input);
|
||||
|
||||
if (validatedInput.thoughtNumber > validatedInput.totalThoughts) {
|
||||
validatedInput.totalThoughts = validatedInput.thoughtNumber;
|
||||
}
|
||||
|
||||
this.thoughtHistory.push(validatedInput);
|
||||
|
||||
if (validatedInput.branchFromThought && validatedInput.branchId) {
|
||||
if (!this.branches[validatedInput.branchId]) {
|
||||
this.branches[validatedInput.branchId] = [];
|
||||
}
|
||||
this.branches[validatedInput.branchId].push(validatedInput);
|
||||
}
|
||||
|
||||
if (!this.disableThoughtLogging) {
|
||||
const formattedThought = this.formatThought(validatedInput);
|
||||
console.error(formattedThought);
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: JSON.stringify({
|
||||
thoughtNumber: validatedInput.thoughtNumber,
|
||||
totalThoughts: validatedInput.totalThoughts,
|
||||
nextThoughtNeeded: validatedInput.nextThoughtNeeded,
|
||||
branches: Object.keys(this.branches),
|
||||
thoughtHistoryLength: this.thoughtHistory.length
|
||||
}, null, 2)
|
||||
}]
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: JSON.stringify({
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
status: 'failed'
|
||||
}, null, 2)
|
||||
}],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,17 +16,23 @@
|
||||
"scripts": {
|
||||
"build": "tsc && shx chmod +x dist/*.js",
|
||||
"prepare": "npm run build",
|
||||
"watch": "tsc --watch"
|
||||
"watch": "tsc --watch",
|
||||
"test": "jest --config=jest.config.cjs --coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "0.5.0",
|
||||
"@modelcontextprotocol/sdk": "^1.19.1",
|
||||
"chalk": "^5.3.0",
|
||||
"yargs": "^17.7.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@types/jest": "^29.5.14",
|
||||
"@types/node": "^22",
|
||||
"@types/yargs": "^17.0.32",
|
||||
"jest": "^29.7.0",
|
||||
"shx": "^0.3.4",
|
||||
"ts-jest": "^29.1.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.3.3"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user