mirror of
https://github.com/modelcontextprotocol/servers.git
synced 2026-04-18 16:13:22 +02:00
Merge branch 'main' into claude/issue-2361-20250817-1626
This commit is contained in:
@@ -124,7 +124,7 @@ Resource features:
|
||||
2. `complex_prompt`
|
||||
- Advanced prompt demonstrating argument handling
|
||||
- Required arguments:
|
||||
- `temperature` (number): Temperature setting
|
||||
- `temperature` (string): Temperature setting
|
||||
- Optional arguments:
|
||||
- `style` (string): Output style preference
|
||||
- Returns: Multi-turn conversation with images
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
||||
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
||||
import {
|
||||
CallToolRequestSchema,
|
||||
ClientCapabilities,
|
||||
CompleteRequestSchema,
|
||||
CreateMessageRequest,
|
||||
CreateMessageResultSchema,
|
||||
ElicitResultSchema,
|
||||
GetPromptRequestSchema,
|
||||
ListPromptsRequestSchema,
|
||||
ListResourcesRequestSchema,
|
||||
@@ -14,18 +16,20 @@ import {
|
||||
ReadResourceRequestSchema,
|
||||
Resource,
|
||||
RootsListChangedNotificationSchema,
|
||||
SetLevelRequestSchema,
|
||||
ServerNotification,
|
||||
ServerRequest,
|
||||
SubscribeRequestSchema,
|
||||
Tool,
|
||||
ToolSchema,
|
||||
UnsubscribeRequestSchema,
|
||||
type Root,
|
||||
type Root
|
||||
} from "@modelcontextprotocol/sdk/types.js";
|
||||
import { z } from "zod";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import { readFileSync } from "fs";
|
||||
import { fileURLToPath } from "url";
|
||||
import { dirname, join } from "path";
|
||||
import JSZip from "jszip";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
@@ -37,6 +41,8 @@ type ToolInput = z.infer<typeof ToolInputSchema>;
|
||||
const ToolOutputSchema = ToolSchema.shape.outputSchema;
|
||||
type ToolOutput = z.infer<typeof ToolOutputSchema>;
|
||||
|
||||
type SendRequest = RequestHandlerExtra<ServerRequest, ServerNotification>["sendRequest"];
|
||||
|
||||
/* Input schemas for tools implemented in this server */
|
||||
const EchoSchema = z.object({
|
||||
message: z.string().describe("Message to echo"),
|
||||
@@ -123,6 +129,10 @@ const StructuredContentSchema = {
|
||||
})
|
||||
};
|
||||
|
||||
const ZipResourcesInputSchema = z.object({
|
||||
files: z.record(z.string().url().describe("URL of the file to include in the zip")).describe("Mapping of file names to URLs to include in the zip"),
|
||||
});
|
||||
|
||||
enum ToolName {
|
||||
ECHO = "echo",
|
||||
ADD = "add",
|
||||
@@ -135,6 +145,7 @@ enum ToolName {
|
||||
ELICITATION = "startElicitation",
|
||||
GET_RESOURCE_LINKS = "getResourceLinks",
|
||||
STRUCTURED_CONTENT = "structuredContent",
|
||||
ZIP_RESOURCES = "zip",
|
||||
LIST_ROOTS = "listRoots"
|
||||
}
|
||||
|
||||
@@ -174,7 +185,6 @@ export const createServer = () => {
|
||||
let subsUpdateInterval: NodeJS.Timeout | undefined;
|
||||
let stdErrUpdateInterval: NodeJS.Timeout | undefined;
|
||||
|
||||
let logLevel: LoggingLevel = "debug";
|
||||
let logsUpdateInterval: NodeJS.Timeout | undefined;
|
||||
// Store client capabilities
|
||||
let clientCapabilities: ClientCapabilities | undefined;
|
||||
@@ -182,55 +192,48 @@ export const createServer = () => {
|
||||
// Roots state management
|
||||
let currentRoots: Root[] = [];
|
||||
let clientSupportsRoots = false;
|
||||
const messages = [
|
||||
{ level: "debug", data: "Debug-level message" },
|
||||
{ level: "info", data: "Info-level message" },
|
||||
{ level: "notice", data: "Notice-level message" },
|
||||
{ level: "warning", data: "Warning-level message" },
|
||||
{ level: "error", data: "Error-level message" },
|
||||
{ level: "critical", data: "Critical-level message" },
|
||||
{ level: "alert", data: "Alert level-message" },
|
||||
{ level: "emergency", data: "Emergency-level message" },
|
||||
];
|
||||
let sessionId: string | undefined;
|
||||
|
||||
const isMessageIgnored = (level: LoggingLevel): boolean => {
|
||||
const currentLevel = messages.findIndex((msg) => logLevel === msg.level);
|
||||
const messageLevel = messages.findIndex((msg) => level === msg.level);
|
||||
return messageLevel < currentLevel;
|
||||
};
|
||||
// Function to start notification intervals when a client connects
|
||||
const startNotificationIntervals = (sid?: string|undefined) => {
|
||||
sessionId = sid;
|
||||
if (!subsUpdateInterval) {
|
||||
subsUpdateInterval = setInterval(() => {
|
||||
for (const uri of subscriptions) {
|
||||
server.notification({
|
||||
method: "notifications/resources/updated",
|
||||
params: { uri },
|
||||
});
|
||||
}
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
// Function to start notification intervals when a client connects
|
||||
const startNotificationIntervals = () => {
|
||||
if (!subsUpdateInterval) {
|
||||
subsUpdateInterval = setInterval(() => {
|
||||
for (const uri of subscriptions) {
|
||||
server.notification({
|
||||
method: "notifications/resources/updated",
|
||||
params: { uri },
|
||||
});
|
||||
}
|
||||
}, 10000);
|
||||
}
|
||||
const maybeAppendSessionId = sessionId ? ` - SessionId ${sessionId}`: "";
|
||||
const messages: { level: LoggingLevel; data: string }[] = [
|
||||
{ level: "debug", data: `Debug-level message${maybeAppendSessionId}` },
|
||||
{ level: "info", data: `Info-level message${maybeAppendSessionId}` },
|
||||
{ level: "notice", data: `Notice-level message${maybeAppendSessionId}` },
|
||||
{ level: "warning", data: `Warning-level message${maybeAppendSessionId}` },
|
||||
{ level: "error", data: `Error-level message${maybeAppendSessionId}` },
|
||||
{ level: "critical", data: `Critical-level message${maybeAppendSessionId}` },
|
||||
{ level: "alert", data: `Alert level-message${maybeAppendSessionId}` },
|
||||
{ level: "emergency", data: `Emergency-level message${maybeAppendSessionId}` },
|
||||
];
|
||||
|
||||
if (!logsUpdateInterval) {
|
||||
logsUpdateInterval = setInterval(() => {
|
||||
let message = {
|
||||
method: "notifications/message",
|
||||
params: messages[Math.floor(Math.random() * messages.length)],
|
||||
};
|
||||
if (!isMessageIgnored(message.params.level as LoggingLevel))
|
||||
server.notification(message);
|
||||
}, 20000);
|
||||
if (!logsUpdateInterval) {
|
||||
console.error("Starting logs update interval");
|
||||
logsUpdateInterval = setInterval(async () => {
|
||||
await server.sendLoggingMessage( messages[Math.floor(Math.random() * messages.length)], sessionId);
|
||||
}, 15000);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Helper method to request sampling from client
|
||||
const requestSampling = async (
|
||||
context: string,
|
||||
uri: string,
|
||||
maxTokens: number = 100
|
||||
maxTokens: number = 100,
|
||||
sendRequest: SendRequest
|
||||
) => {
|
||||
const request: CreateMessageRequest = {
|
||||
method: "sampling/createMessage",
|
||||
@@ -251,22 +254,8 @@ export const createServer = () => {
|
||||
},
|
||||
};
|
||||
|
||||
return await server.request(request, CreateMessageResultSchema);
|
||||
};
|
||||
return await sendRequest(request, CreateMessageResultSchema);
|
||||
|
||||
const requestElicitation = async (
|
||||
message: string,
|
||||
requestedSchema: any
|
||||
) => {
|
||||
const request = {
|
||||
method: 'elicitation/create',
|
||||
params: {
|
||||
message,
|
||||
requestedSchema
|
||||
}
|
||||
};
|
||||
|
||||
return await server.request(request, z.any());
|
||||
};
|
||||
|
||||
const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => {
|
||||
@@ -344,12 +333,9 @@ export const createServer = () => {
|
||||
throw new Error(`Unknown resource: ${uri}`);
|
||||
});
|
||||
|
||||
server.setRequestHandler(SubscribeRequestSchema, async (request) => {
|
||||
server.setRequestHandler(SubscribeRequestSchema, async (request, extra) => {
|
||||
const { uri } = request.params;
|
||||
subscriptions.add(uri);
|
||||
|
||||
// Request sampling from client when someone subscribes
|
||||
await requestSampling("A new subscription was started", uri);
|
||||
return {};
|
||||
});
|
||||
|
||||
@@ -535,6 +521,11 @@ export const createServer = () => {
|
||||
inputSchema: zodToJsonSchema(StructuredContentSchema.input) as ToolInput,
|
||||
outputSchema: zodToJsonSchema(StructuredContentSchema.output) as ToolOutput,
|
||||
},
|
||||
{
|
||||
name: ToolName.ZIP_RESOURCES,
|
||||
description: "Compresses the provided resource files (mapping of name to URI, which can be a data URI) to a zip file, which it returns as a data URI resource link.",
|
||||
inputSchema: zodToJsonSchema(ZipResourcesInputSchema) as ToolInput,
|
||||
}
|
||||
];
|
||||
if (clientCapabilities!.roots) tools.push ({
|
||||
name: ToolName.LIST_ROOTS,
|
||||
@@ -544,14 +535,14 @@ export const createServer = () => {
|
||||
});
|
||||
if (clientCapabilities!.elicitation) tools.push ({
|
||||
name: ToolName.ELICITATION,
|
||||
description: "Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.",
|
||||
description: "Elicitation test tool that demonstrates how to request user input with various field types (string, boolean, email, uri, date, integer, number, enum)",
|
||||
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
|
||||
});
|
||||
|
||||
return { tools };
|
||||
});
|
||||
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request,extra) => {
|
||||
const { name, arguments: args } = request.params;
|
||||
|
||||
if (name === ToolName.ECHO) {
|
||||
@@ -593,7 +584,7 @@ export const createServer = () => {
|
||||
total: steps,
|
||||
progressToken,
|
||||
},
|
||||
});
|
||||
},{relatedRequestId: extra.requestId});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -625,7 +616,8 @@ export const createServer = () => {
|
||||
const result = await requestSampling(
|
||||
prompt,
|
||||
ToolName.SAMPLE_LLM,
|
||||
maxTokens
|
||||
maxTokens,
|
||||
extra.sendRequest
|
||||
);
|
||||
return {
|
||||
content: [
|
||||
@@ -738,21 +730,75 @@ export const createServer = () => {
|
||||
if (name === ToolName.ELICITATION) {
|
||||
ElicitationSchema.parse(args);
|
||||
|
||||
const elicitationResult = await requestElicitation(
|
||||
'What are your favorite things?',
|
||||
{
|
||||
type: 'object',
|
||||
properties: {
|
||||
color: { type: 'string', description: 'Favorite color' },
|
||||
number: { type: 'integer', description: 'Favorite number', minimum: 1, maximum: 100 },
|
||||
pets: {
|
||||
type: 'string',
|
||||
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'],
|
||||
description: 'Favorite pets'
|
||||
const elicitationResult = await extra.sendRequest({
|
||||
method: 'elicitation/create',
|
||||
params: {
|
||||
message: 'Please provide inputs for the following fields:',
|
||||
requestedSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
title: 'Full Name',
|
||||
type: 'string',
|
||||
description: 'Your full, legal name',
|
||||
},
|
||||
check: {
|
||||
title: 'Agree to terms',
|
||||
type: 'boolean',
|
||||
description: 'A boolean check',
|
||||
},
|
||||
color: {
|
||||
title: 'Favorite Color',
|
||||
type: 'string',
|
||||
description: 'Favorite color (open text)',
|
||||
default: 'blue',
|
||||
},
|
||||
email: {
|
||||
title: 'Email Address',
|
||||
type: 'string',
|
||||
format: 'email',
|
||||
description: 'Your email address (will be verified, and never shared with anyone else)',
|
||||
},
|
||||
homepage: {
|
||||
type: 'string',
|
||||
format: 'uri',
|
||||
description: 'Homepage / personal site',
|
||||
},
|
||||
birthdate: {
|
||||
title: 'Birthdate',
|
||||
type: 'string',
|
||||
format: 'date',
|
||||
description: 'Your date of birth (will never be shared with anyone else)',
|
||||
},
|
||||
integer: {
|
||||
title: 'Favorite Integer',
|
||||
type: 'integer',
|
||||
description: 'Your favorite integer (do not give us your phone number, pin, or other sensitive info)',
|
||||
minimum: 1,
|
||||
maximum: 100,
|
||||
default: 42,
|
||||
},
|
||||
number: {
|
||||
title: 'Favorite Number',
|
||||
type: 'number',
|
||||
description: 'Favorite number (there are no wrong answers)',
|
||||
minimum: 0,
|
||||
maximum: 1000,
|
||||
default: 3.14,
|
||||
},
|
||||
petType: {
|
||||
title: 'Pet type',
|
||||
type: 'string',
|
||||
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'],
|
||||
enumNames: ['Cats', 'Dogs', 'Birds', 'Fish', 'Reptiles'],
|
||||
default: 'dogs',
|
||||
description: 'Your favorite pet type',
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
);
|
||||
required: ['name'],
|
||||
},
|
||||
},
|
||||
}, ElicitResultSchema, { timeout: 10 * 60 * 1000 /* 10 minutes */ });
|
||||
|
||||
// Handle different response actions
|
||||
const content = [];
|
||||
@@ -760,19 +806,30 @@ export const createServer = () => {
|
||||
if (elicitationResult.action === 'accept' && elicitationResult.content) {
|
||||
content.push({
|
||||
type: "text",
|
||||
text: `✅ User provided their favorite things!`,
|
||||
text: `✅ User provided the requested information!`,
|
||||
});
|
||||
|
||||
// Only access elicitationResult.content when action is accept
|
||||
const { color, number, pets } = elicitationResult.content;
|
||||
const userData = elicitationResult.content;
|
||||
const lines = [];
|
||||
if (userData.name) lines.push(`- Name: ${userData.name}`);
|
||||
if (userData.check !== undefined) lines.push(`- Agreed to terms: ${userData.check}`);
|
||||
if (userData.color) lines.push(`- Favorite Color: ${userData.color}`);
|
||||
if (userData.email) lines.push(`- Email: ${userData.email}`);
|
||||
if (userData.homepage) lines.push(`- Homepage: ${userData.homepage}`);
|
||||
if (userData.birthdate) lines.push(`- Birthdate: ${userData.birthdate}`);
|
||||
if (userData.integer !== undefined) lines.push(`- Favorite Integer: ${userData.integer}`);
|
||||
if (userData.number !== undefined) lines.push(`- Favorite Number: ${userData.number}`);
|
||||
if (userData.petType) lines.push(`- Pet Type: ${userData.petType}`);
|
||||
|
||||
content.push({
|
||||
type: "text",
|
||||
text: `Their favorites are:\n- Color: ${color || 'not specified'}\n- Number: ${number || 'not specified'}\n- Pets: ${pets || 'not specified'}`,
|
||||
text: `User inputs:\n${lines.join('\n')}`,
|
||||
});
|
||||
} else if (elicitationResult.action === 'decline') {
|
||||
content.push({
|
||||
type: "text",
|
||||
text: `❌ User declined to provide their favorite things.`,
|
||||
text: `❌ User declined to provide the requested information.`,
|
||||
});
|
||||
} else if (elicitationResult.action === 'cancel') {
|
||||
content.push({
|
||||
@@ -840,6 +897,37 @@ export const createServer = () => {
|
||||
};
|
||||
}
|
||||
|
||||
if (name === ToolName.ZIP_RESOURCES) {
|
||||
const { files } = ZipResourcesInputSchema.parse(args);
|
||||
|
||||
const zip = new JSZip();
|
||||
|
||||
for (const [fileName, fileUrl] of Object.entries(files)) {
|
||||
try {
|
||||
const response = await fetch(fileUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch ${fileUrl}: ${response.statusText}`);
|
||||
}
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
zip.file(fileName, arrayBuffer);
|
||||
} catch (error) {
|
||||
throw new Error(`Error fetching file ${fileUrl}: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
const uri = `data:application/zip;base64,${await zip.generateAsync({ type: "base64" })}`;
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "resource_link",
|
||||
mimeType: "application/zip",
|
||||
uri,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
if (name === ToolName.LIST_ROOTS) {
|
||||
ListRootsSchema.parse(args);
|
||||
|
||||
@@ -918,23 +1006,6 @@ export const createServer = () => {
|
||||
throw new Error(`Unknown reference type`);
|
||||
});
|
||||
|
||||
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
||||
const { level } = request.params;
|
||||
logLevel = level;
|
||||
|
||||
// Demonstrate different log levels
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
level: "debug",
|
||||
logger: "test-server",
|
||||
data: `Logging level set to: ${logLevel}`,
|
||||
},
|
||||
});
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
// Roots protocol handlers
|
||||
server.setNotificationHandler(RootsListChangedNotificationSchema, async () => {
|
||||
try {
|
||||
@@ -944,24 +1015,18 @@ export const createServer = () => {
|
||||
currentRoots = response.roots;
|
||||
|
||||
// Log the roots update for demonstration
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
await server.sendLoggingMessage({
|
||||
level: "info",
|
||||
logger: "everything-server",
|
||||
data: `Roots updated: ${currentRoots.length} root(s) received from client`,
|
||||
},
|
||||
});
|
||||
}, sessionId);
|
||||
}
|
||||
} catch (error) {
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
await server.sendLoggingMessage({
|
||||
level: "error",
|
||||
logger: "everything-server",
|
||||
data: `Failed to request roots from client: ${error instanceof Error ? error.message : String(error)}`,
|
||||
},
|
||||
});
|
||||
}, sessionId);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -976,43 +1041,31 @@ export const createServer = () => {
|
||||
if (response && 'roots' in response) {
|
||||
currentRoots = response.roots;
|
||||
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
await server.sendLoggingMessage({
|
||||
level: "info",
|
||||
logger: "everything-server",
|
||||
data: `Initial roots received: ${currentRoots.length} root(s) from client`,
|
||||
},
|
||||
});
|
||||
}, sessionId);
|
||||
} else {
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
await server.sendLoggingMessage({
|
||||
level: "warning",
|
||||
logger: "everything-server",
|
||||
data: "Client returned no roots set",
|
||||
},
|
||||
});
|
||||
}, sessionId);
|
||||
}
|
||||
} catch (error) {
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
await server.sendLoggingMessage({
|
||||
level: "error",
|
||||
logger: "everything-server",
|
||||
data: `Failed to request initial roots from client: ${error instanceof Error ? error.message : String(error)}`,
|
||||
},
|
||||
});
|
||||
}, sessionId);
|
||||
}
|
||||
} else {
|
||||
await server.notification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
await server.sendLoggingMessage({
|
||||
level: "info",
|
||||
logger: "everything-server",
|
||||
data: "Client does not support MCP roots protocol",
|
||||
},
|
||||
});
|
||||
}, sessionId);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -22,12 +22,15 @@
|
||||
"start:streamableHttp": "node dist/streamableHttp.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.17.4",
|
||||
"@modelcontextprotocol/sdk": "^1.19.1",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.21.1",
|
||||
"jszip": "^3.10.1",
|
||||
"zod": "^3.23.8",
|
||||
"zod-to-json-schema": "^3.23.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.19",
|
||||
"@types/express": "^5.0.0",
|
||||
"shx": "^0.3.4",
|
||||
"typescript": "^5.6.2"
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
||||
import express from "express";
|
||||
import { createServer } from "./everything.js";
|
||||
import cors from 'cors';
|
||||
|
||||
console.error('Starting SSE server...');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors({
|
||||
"origin": "*", // use "*" with caution in production
|
||||
"methods": "GET,POST",
|
||||
"preflightContinue": false,
|
||||
"optionsSuccessStatus": 204,
|
||||
})); // Enable CORS for all routes so Inspector can connect
|
||||
const transports: Map<string, SSEServerTransport> = new Map<string, SSEServerTransport>();
|
||||
|
||||
app.get("/sse", async (req, res) => {
|
||||
@@ -26,7 +32,7 @@ app.get("/sse", async (req, res) => {
|
||||
console.error("Client Connected: ", transport.sessionId);
|
||||
|
||||
// Start notification intervals after client connects
|
||||
startNotificationIntervals();
|
||||
startNotificationIntervals(transport.sessionId);
|
||||
|
||||
// Handle close of connection
|
||||
server.onclose = async () => {
|
||||
|
||||
@@ -6,17 +6,18 @@ import { createServer } from "./everything.js";
|
||||
console.error('Starting default (STDIO) server...');
|
||||
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
const {server, cleanup} = createServer();
|
||||
const transport = new StdioServerTransport();
|
||||
const {server, cleanup, startNotificationIntervals} = createServer();
|
||||
|
||||
await server.connect(transport);
|
||||
await server.connect(transport);
|
||||
startNotificationIntervals();
|
||||
|
||||
// Cleanup on exit
|
||||
process.on("SIGINT", async () => {
|
||||
await cleanup();
|
||||
await server.close();
|
||||
process.exit(0);
|
||||
});
|
||||
// Cleanup on exit
|
||||
process.on("SIGINT", async () => {
|
||||
await cleanup();
|
||||
await server.close();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
|
||||
@@ -3,10 +3,22 @@ import { InMemoryEventStore } from '@modelcontextprotocol/sdk/examples/shared/in
|
||||
import express, { Request, Response } from "express";
|
||||
import { createServer } from "./everything.js";
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import cors from 'cors';
|
||||
|
||||
console.error('Starting Streamable HTTP server...');
|
||||
|
||||
const app = express();
|
||||
app.use(cors({
|
||||
"origin": "*", // use "*" with caution in production
|
||||
"methods": "GET,POST,DELETE",
|
||||
"preflightContinue": false,
|
||||
"optionsSuccessStatus": 204,
|
||||
"exposedHeaders": [
|
||||
'mcp-session-id',
|
||||
'last-event-id',
|
||||
'mcp-protocol-version'
|
||||
]
|
||||
})); // Enable CORS for all routes so Inspector can connect
|
||||
|
||||
const transports: Map<string, StreamableHTTPServerTransport> = new Map<string, StreamableHTTPServerTransport>();
|
||||
|
||||
@@ -15,6 +27,7 @@ app.post('/mcp', async (req: Request, res: Response) => {
|
||||
try {
|
||||
// Check for existing session ID
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
|
||||
let transport: StreamableHTTPServerTransport;
|
||||
|
||||
if (sessionId && transports.has(sessionId)) {
|
||||
@@ -22,7 +35,7 @@ app.post('/mcp', async (req: Request, res: Response) => {
|
||||
transport = transports.get(sessionId)!;
|
||||
} else if (!sessionId) {
|
||||
|
||||
const { server, cleanup } = createServer();
|
||||
const { server, cleanup, startNotificationIntervals } = createServer();
|
||||
|
||||
// New initialization request
|
||||
const eventStore = new InMemoryEventStore();
|
||||
@@ -53,7 +66,11 @@ app.post('/mcp', async (req: Request, res: Response) => {
|
||||
await server.connect(transport);
|
||||
|
||||
await transport.handleRequest(req, res);
|
||||
return; // Already handled
|
||||
|
||||
// Wait until initialize is complete and transport will have a sessionId
|
||||
startNotificationIntervals(transport.sessionId);
|
||||
|
||||
return; // Already handled
|
||||
} else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
|
||||
@@ -128,15 +128,6 @@ The server's directory access control follows this flow:
|
||||
- Returns detailed listing with file sizes and summary statistics
|
||||
- Shows total files, directories, and combined size
|
||||
|
||||
- **directory_tree**
|
||||
- Get a recursive tree view of files and directories as a JSON structure
|
||||
- Input: `path` (string): Starting directory path
|
||||
- Returns JSON structure with:
|
||||
- `name`: File/directory name
|
||||
- `type`: "file" or "directory"
|
||||
- `children`: Array of child entries (for directories only)
|
||||
- Output is formatted with 2-space indentation for readability
|
||||
|
||||
- **move_file**
|
||||
- Move or rename files and directories
|
||||
- Inputs:
|
||||
@@ -165,6 +156,7 @@ The server's directory access control follows this flow:
|
||||
- `children` (array): Present only for directories
|
||||
- Empty array for empty directories
|
||||
- Omitted for files
|
||||
- Output is formatted with 2-space indentation for readability
|
||||
|
||||
- **get_file_info**
|
||||
- Get detailed file/directory metadata
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -84,13 +84,8 @@ Please note that mcp-server-git is currently in early development. The functiona
|
||||
- `repo_path` (string): Path to Git repository
|
||||
- `revision` (string): The revision (commit hash, branch name, tag) to show
|
||||
- Returns: Contents of the specified commit
|
||||
12. `git_init`
|
||||
- Initializes a Git repository
|
||||
- Inputs:
|
||||
- `repo_path` (string): Path to directory to initialize git repo
|
||||
- Returns: Confirmation of repository initialization
|
||||
|
||||
13. `git_branch`
|
||||
12. `git_branch`
|
||||
- List Git branches
|
||||
- Inputs:
|
||||
- `repo_path` (string): Path to the Git repository.
|
||||
|
||||
@@ -70,8 +70,7 @@ class GitShow(BaseModel):
|
||||
repo_path: str
|
||||
revision: str
|
||||
|
||||
class GitInit(BaseModel):
|
||||
repo_path: str
|
||||
|
||||
|
||||
class GitBranch(BaseModel):
|
||||
repo_path: str = Field(
|
||||
@@ -104,7 +103,7 @@ class GitTools(str, Enum):
|
||||
CREATE_BRANCH = "git_create_branch"
|
||||
CHECKOUT = "git_checkout"
|
||||
SHOW = "git_show"
|
||||
INIT = "git_init"
|
||||
|
||||
BRANCH = "git_branch"
|
||||
|
||||
def git_status(repo: git.Repo) -> str:
|
||||
@@ -183,12 +182,7 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str:
|
||||
repo.git.checkout(branch_name)
|
||||
return f"Switched to branch '{branch_name}'"
|
||||
|
||||
def git_init(repo_path: str) -> str:
|
||||
try:
|
||||
repo = git.Repo.init(path=repo_path, mkdir=True)
|
||||
return f"Initialized empty Git repository in {repo.git_dir}"
|
||||
except Exception as e:
|
||||
return f"Error initializing repository: {str(e)}"
|
||||
|
||||
|
||||
def git_show(repo: git.Repo, revision: str) -> str:
|
||||
commit = repo.commit(revision)
|
||||
@@ -308,11 +302,7 @@ async def serve(repository: Path | None) -> None:
|
||||
description="Shows the contents of a commit",
|
||||
inputSchema=GitShow.model_json_schema(),
|
||||
),
|
||||
Tool(
|
||||
name=GitTools.INIT,
|
||||
description="Initialize a new Git repository",
|
||||
inputSchema=GitInit.model_json_schema(),
|
||||
),
|
||||
|
||||
Tool(
|
||||
name=GitTools.BRANCH,
|
||||
description="List Git branches",
|
||||
@@ -354,15 +344,7 @@ async def serve(repository: Path | None) -> None:
|
||||
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
|
||||
repo_path = Path(arguments["repo_path"])
|
||||
|
||||
# Handle git init separately since it doesn't require an existing repo
|
||||
if name == GitTools.INIT:
|
||||
result = git_init(str(repo_path))
|
||||
return [TextContent(
|
||||
type="text",
|
||||
text=result
|
||||
)]
|
||||
|
||||
# For all other commands, we need an existing repo
|
||||
# For all commands, we need an existing repo
|
||||
repo = git.Repo(repo_path)
|
||||
|
||||
match name:
|
||||
|
||||
@@ -89,8 +89,18 @@ class KnowledgeGraphManager {
|
||||
|
||||
private async saveGraph(graph: KnowledgeGraph): Promise<void> {
|
||||
const lines = [
|
||||
...graph.entities.map(e => JSON.stringify({ type: "entity", ...e })),
|
||||
...graph.relations.map(r => JSON.stringify({ type: "relation", ...r })),
|
||||
...graph.entities.map(e => JSON.stringify({
|
||||
type: "entity",
|
||||
name: e.name,
|
||||
entityType: e.entityType,
|
||||
observations: e.observations
|
||||
})),
|
||||
...graph.relations.map(r => JSON.stringify({
|
||||
type: "relation",
|
||||
from: r.from,
|
||||
to: r.to,
|
||||
relationType: r.relationType
|
||||
})),
|
||||
];
|
||||
await fs.writeFile(MEMORY_FILE_PATH, lines.join("\n"));
|
||||
}
|
||||
@@ -248,10 +258,12 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
},
|
||||
},
|
||||
required: ["name", "entityType", "observations"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ["entities"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -270,10 +282,12 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
relationType: { type: "string", description: "The type of the relation" },
|
||||
},
|
||||
required: ["from", "to", "relationType"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ["relations"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -295,10 +309,12 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
},
|
||||
},
|
||||
required: ["entityName", "contents"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ["observations"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -314,6 +330,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
},
|
||||
},
|
||||
required: ["entityNames"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -335,10 +352,12 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
},
|
||||
},
|
||||
required: ["entityName", "observations"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ["deletions"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -357,11 +376,13 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
relationType: { type: "string", description: "The type of the relation" },
|
||||
},
|
||||
required: ["from", "to", "relationType"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
description: "An array of relations to delete"
|
||||
},
|
||||
},
|
||||
required: ["relations"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -370,6 +391,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {},
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -381,6 +403,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
query: { type: "string", description: "The search query to match against entity names, types, and observation content" },
|
||||
},
|
||||
required: ["query"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -396,6 +419,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
},
|
||||
},
|
||||
required: ["names"],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,8 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
|
||||
local_tzname = get_localzone_name()
|
||||
if local_tzname is not None:
|
||||
return ZoneInfo(local_tzname)
|
||||
raise McpError("Could not determine local timezone - tzinfo is None")
|
||||
# Default to UTC if local timezone cannot be determined
|
||||
return ZoneInfo("UTC")
|
||||
|
||||
|
||||
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
|
||||
|
||||
@@ -486,10 +486,10 @@ def test_get_local_tz_with_valid_iana_name(mock_get_localzone):
|
||||
|
||||
@patch('mcp_server_time.server.get_localzone_name')
|
||||
def test_get_local_tz_when_none_returned(mock_get_localzone):
|
||||
"""Test error when tzlocal returns None."""
|
||||
"""Test default to UTC when tzlocal returns None."""
|
||||
mock_get_localzone.return_value = None
|
||||
with pytest.raises(McpError, match="Could not determine local timezone"):
|
||||
get_local_tz()
|
||||
result = get_local_tz()
|
||||
assert str(result) == "UTC"
|
||||
|
||||
|
||||
@patch('mcp_server_time.server.get_localzone_name')
|
||||
|
||||
Reference in New Issue
Block a user