-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
166 lines (147 loc) · 4.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
Tool,
} from "@modelcontextprotocol/sdk/types.js";
import chalk from 'chalk';
class ReasonerServer {
constructor() {
this.thoughts = [];
this.branches = {};
}
validateInput(input) {
const data = input;
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 true;
}
formatThought(thoughtData) {
const { thoughtNumber, totalThoughts, thought } = thoughtData;
const prefix = chalk.blue('🤔 Reasoning');
const header = `${prefix} ${thoughtNumber}/${totalThoughts}`;
const border = '─'.repeat(Math.max(header.length, thought.length) + 4);
return `
┌${border}┐
│ ${header.padEnd(border.length - 2)} │
├${border}┤
│ ${thought.padEnd(border.length - 2)} │
└${border}┘`;
}
processThought(input) {
try {
this.validateInput(input);
// Adjust total thoughts if needed
if (input.thoughtNumber > input.totalThoughts) {
input.totalThoughts = input.thoughtNumber;
}
// Add to history
this.thoughts.push(input);
// Format and display
const formattedThought = this.formatThought(input);
console.error(formattedThought);
return {
content: [{
type: "text",
text: JSON.stringify({
thoughtNumber: input.thoughtNumber,
totalThoughts: input.totalThoughts,
nextThoughtNeeded: input.nextThoughtNeeded,
thoughtCount: this.thoughts.length
}, null, 2)
}]
};
} catch (error) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: error.message,
status: 'failed'
}, null, 2)
}],
isError: true
};
}
}
}
const REASONER_TOOL = {
name: "reasoner",
description: "A reasoning engine that helps break down and analyze problems step by step",
inputSchema: {
type: "object",
properties: {
thought: {
type: "string",
description: "The current reasoning step"
},
thoughtNumber: {
type: "integer",
description: "Current step number",
minimum: 1
},
totalThoughts: {
type: "integer",
description: "Estimated total steps needed",
minimum: 1
},
nextThoughtNeeded: {
type: "boolean",
description: "Whether another step is needed"
}
},
required: ["thought", "thoughtNumber", "totalThoughts", "nextThoughtNeeded"]
}
};
// Initialize MCP server
const server = new Server(
{
name: "reasoner-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
const reasonerServer = new ReasonerServer();
// Register tool listing handler
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [REASONER_TOOL],
}));
// Register tool execution handler
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "reasoner") {
return reasonerServer.processThought(request.params.arguments);
}
return {
content: [{
type: "text",
text: `Unknown tool: ${request.params.name}`
}],
isError: true
};
});
// Start the server
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Reasoner MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});