mirror of
https://github.com/Mail-0/Zero.git
synced 2026-06-30 07:46:15 +00:00
Implement sequential thinking MCP and register in agent (#1810)
# Implement Sequential Thinking MCP
This PR implements the Sequential Thinking MCP, a tool for dynamic and reflective problem-solving through a flexible thinking process. The implementation:
1. Activates the previously commented-out sequential thinking tool in the ThinkingMCP class
2. Registers the ThinkingMCP in the worker entrypoint
3. Adds proper SSE endpoint for the thinking MCP
4. Connects the agent to the thinking MCP and enables its AI tools
5. Updates Durable Object configurations in wrangler.jsonc for all environments
## Type of Change
- [x] ✨ New feature (non-breaking change which adds functionality)
## Areas Affected
- [x] API Endpoints
- [x] Development Workflow
- [x] Deployment/Infrastructure
## Testing Done
- [x] Manual testing performed
## Checklist
- [x] I have performed a self-review of my code
- [x] My changes generate no new warnings
- [x] All tests pass locally
## Additional Notes
The Sequential Thinking tool provides a structured approach to problem-solving that can adapt and evolve as understanding deepens. It allows for revising previous thoughts, branching into new paths, and adjusting the thinking process as needed.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||
import type { env } from 'cloudflare:workers';
|
||||
import { McpAgent } from 'agents/mcp';
|
||||
import z from 'zod';
|
||||
|
||||
interface ThoughtData {
|
||||
thought: string;
|
||||
@@ -172,7 +173,7 @@ export class SequentialThinkingProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
export class ThinkingMCP extends McpAgent<typeof env, Record<string, unknown>, { userId: string }> {
|
||||
export class ThinkingMCP extends McpAgent<typeof env> {
|
||||
thinkingServer = new SequentialThinkingProcessor();
|
||||
server = new McpServer({
|
||||
name: 'thinking-mcp',
|
||||
@@ -181,106 +182,94 @@ export class ThinkingMCP extends McpAgent<typeof env, Record<string, unknown>, {
|
||||
});
|
||||
|
||||
async init(): Promise<void> {
|
||||
this.server.tool('Test', () => {
|
||||
return {
|
||||
content: [{ type: 'text' as const, text: 'Hello World' }],
|
||||
};
|
||||
});
|
||||
|
||||
console.log('Here!');
|
||||
|
||||
// this.server.registerTool(
|
||||
// 'sequentialthinking',
|
||||
// {
|
||||
// description: `A detailed tool for dynamic and reflective problem-solving through thoughts.
|
||||
// This tool helps analyze problems through a flexible thinking process that can adapt and evolve.
|
||||
// Each thought can build on, question, or revise previous insights as understanding deepens.
|
||||
|
||||
// When to use this tool:
|
||||
// - Breaking down complex problems into steps
|
||||
// - Planning and design with room for revision
|
||||
// - Analysis that might need course correction
|
||||
// - Problems where the full scope might not be clear initially
|
||||
// - Problems that require a multi-step solution
|
||||
// - Tasks that need to maintain context over multiple steps
|
||||
// - Situations where irrelevant information needs to be filtered out
|
||||
|
||||
// Key features:
|
||||
// - You can adjust total_thoughts up or down as you progress
|
||||
// - You can question or revise previous thoughts
|
||||
// - You can add more thoughts even after reaching what seemed like the end
|
||||
// - You can express uncertainty and explore alternative approaches
|
||||
// - Not every thought needs to build linearly - you can branch or backtrack
|
||||
// - Generates a solution hypothesis
|
||||
// - Verifies the hypothesis based on the Chain of Thought steps
|
||||
// - Repeats the process until satisfied
|
||||
// - Provides a correct answer
|
||||
|
||||
// Parameters explained:
|
||||
// - thought: Your current thinking step, which can include:
|
||||
// * Regular analytical steps
|
||||
// * Revisions of previous thoughts
|
||||
// * Questions about previous decisions
|
||||
// * Realizations about needing more analysis
|
||||
// * Changes in approach
|
||||
// * Hypothesis generation
|
||||
// * Hypothesis verification
|
||||
// - next_thought_needed: True if you need more thinking, even if at what seemed like the end
|
||||
// - thought_number: Current number in sequence (can go beyond initial total if needed)
|
||||
// - total_thoughts: Current estimate of thoughts needed (can be adjusted up/down)
|
||||
// - is_revision: A boolean indicating if this thought revises previous thinking
|
||||
// - revises_thought: If is_revision is true, which thought number is being reconsidered
|
||||
// - branch_from_thought: If branching, which thought number is the branching point
|
||||
// - branch_id: Identifier for the current branch (if any)
|
||||
// - needs_more_thoughts: If reaching end but realizing more thoughts needed
|
||||
|
||||
// You should:
|
||||
// 1. Start with an initial estimate of needed thoughts, but be ready to adjust
|
||||
// 2. Feel free to question or revise previous thoughts
|
||||
// 3. Don't hesitate to add more thoughts if needed, even at the "end"
|
||||
// 4. Express uncertainty when present
|
||||
// 5. Mark thoughts that revise previous thinking or branch into new paths
|
||||
// 6. Ignore information that is irrelevant to the current step
|
||||
// 7. Generate a solution hypothesis when appropriate
|
||||
// 8. Verify the hypothesis based on the Chain of Thought steps
|
||||
// 9. Repeat the process until satisfied with the solution
|
||||
// 10. Provide a single, ideally correct answer as the final output
|
||||
// 11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached`,
|
||||
// inputSchema: {
|
||||
// thought: z.string().describe('Your current thinking step'),
|
||||
// nextThoughtNeeded: z.boolean().describe('Whether another thought step is needed'),
|
||||
// thoughtNumber: z.number().int().min(1).describe('Current thought number'),
|
||||
// totalThoughts: z.number().int().min(1).describe('Estimated total thoughts needed'),
|
||||
// isRevision: z.boolean().optional().describe('Whether this revises previous thinking'),
|
||||
// revisesThought: z
|
||||
// .number()
|
||||
// .int()
|
||||
// .min(1)
|
||||
// .optional()
|
||||
// .describe('Which thought is being reconsidered'),
|
||||
// branchFromThought: z
|
||||
// .number()
|
||||
// .int()
|
||||
// .min(1)
|
||||
// .optional()
|
||||
// .describe('Branching point thought number'),
|
||||
// branchId: z.string().optional().describe('Branch identifier'),
|
||||
// needsMoreThoughts: z.boolean().optional().describe('If more thoughts are needed'),
|
||||
// },
|
||||
// },
|
||||
// (params) => {
|
||||
// return this.thinkingServer.processThought({
|
||||
// thought: params.thought,
|
||||
// nextThoughtNeeded: params.nextThoughtNeeded,
|
||||
// thoughtNumber: params.thoughtNumber,
|
||||
// totalThoughts: params.totalThoughts,
|
||||
// isRevision: params.isRevision,
|
||||
// revisesThought: params.revisesThought,
|
||||
// branchFromThought: params.branchFromThought,
|
||||
// branchId: params.branchId,
|
||||
// needsMoreThoughts: params.needsMoreThoughts,
|
||||
// });
|
||||
// },
|
||||
// );
|
||||
this.server.registerTool(
|
||||
'sequentialthinking',
|
||||
{
|
||||
description: `A detailed tool for dynamic and reflective problem-solving through thoughts.
|
||||
This tool helps analyze problems through a flexible thinking process that can adapt and evolve.
|
||||
Each thought can build on, question, or revise previous insights as understanding deepens.
|
||||
When to use this tool:
|
||||
- Breaking down complex problems into steps
|
||||
- Planning and design with room for revision
|
||||
- Analysis that might need course correction
|
||||
- Problems where the full scope might not be clear initially
|
||||
- Problems that require a multi-step solution
|
||||
- Tasks that need to maintain context over multiple steps
|
||||
- Situations where irrelevant information needs to be filtered out
|
||||
Key features:
|
||||
- You can adjust total_thoughts up or down as you progress
|
||||
- You can question or revise previous thoughts
|
||||
- You can add more thoughts even after reaching what seemed like the end
|
||||
- You can express uncertainty and explore alternative approaches
|
||||
- Not every thought needs to build linearly - you can branch or backtrack
|
||||
- Generates a solution hypothesis
|
||||
- Verifies the hypothesis based on the Chain of Thought steps
|
||||
- Repeats the process until satisfied
|
||||
- Provides a correct answer
|
||||
Parameters explained:
|
||||
- thought: Your current thinking step, which can include:
|
||||
* Regular analytical steps
|
||||
* Revisions of previous thoughts
|
||||
* Questions about previous decisions
|
||||
* Realizations about needing more analysis
|
||||
* Changes in approach
|
||||
* Hypothesis generation
|
||||
* Hypothesis verification
|
||||
- next_thought_needed: True if you need more thinking, even if at what seemed like the end
|
||||
- thought_number: Current number in sequence (can go beyond initial total if needed)
|
||||
- total_thoughts: Current estimate of thoughts needed (can be adjusted up/down)
|
||||
- is_revision: A boolean indicating if this thought revises previous thinking
|
||||
- revises_thought: If is_revision is true, which thought number is being reconsidered
|
||||
- branch_from_thought: If branching, which thought number is the branching point
|
||||
- branch_id: Identifier for the current branch (if any)
|
||||
- needs_more_thoughts: If reaching end but realizing more thoughts needed
|
||||
You should:
|
||||
1. Start with an initial estimate of needed thoughts, but be ready to adjust
|
||||
2. Feel free to question or revise previous thoughts
|
||||
3. Don't hesitate to add more thoughts if needed, even at the "end"
|
||||
4. Express uncertainty when present
|
||||
5. Mark thoughts that revise previous thinking or branch into new paths
|
||||
6. Ignore information that is irrelevant to the current step
|
||||
7. Generate a solution hypothesis when appropriate
|
||||
8. Verify the hypothesis based on the Chain of Thought steps
|
||||
9. Repeat the process until satisfied with the solution
|
||||
10. Provide a single, ideally correct answer as the final output
|
||||
11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached`,
|
||||
inputSchema: {
|
||||
thought: z.string().describe('Your current thinking step'),
|
||||
nextThoughtNeeded: z.boolean().describe('Whether another thought step is needed'),
|
||||
thoughtNumber: z.number().int().min(1).describe('Current thought number'),
|
||||
totalThoughts: z.number().int().min(1).describe('Estimated total thoughts needed'),
|
||||
isRevision: z.boolean().optional().describe('Whether this revises previous thinking'),
|
||||
revisesThought: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe('Which thought is being reconsidered'),
|
||||
branchFromThought: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe('Branching point thought number'),
|
||||
branchId: z.string().optional().describe('Branch identifier'),
|
||||
needsMoreThoughts: z.boolean().optional().describe('If more thoughts are needed'),
|
||||
},
|
||||
},
|
||||
(params) => {
|
||||
return this.thinkingServer.processThought({
|
||||
thought: params.thought,
|
||||
nextThoughtNeeded: params.nextThoughtNeeded,
|
||||
thoughtNumber: params.thoughtNumber,
|
||||
totalThoughts: params.totalThoughts,
|
||||
isRevision: params.isRevision,
|
||||
revisesThought: params.revisesThought,
|
||||
branchFromThought: params.branchFromThought,
|
||||
branchId: params.branchId,
|
||||
needsMoreThoughts: params.needsMoreThoughts,
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import { oAuthDiscoveryMetadata } from 'better-auth/plugins';
|
||||
import { getZeroDB, verifyToken } from './lib/server-utils';
|
||||
import { eq, and, desc, asc, inArray } from 'drizzle-orm';
|
||||
import { EWorkflowType, runWorkflow } from './pipelines';
|
||||
import { ThinkingMCP } from './lib/sequential-thinking';
|
||||
import { ZeroAgent, ZeroDriver } from './routes/agent';
|
||||
import { contextStorage } from 'hono/context-storage';
|
||||
import { defaultUserSettings } from './lib/schemas';
|
||||
@@ -610,6 +611,17 @@ export default class extends WorkerEntrypoint<typeof env> {
|
||||
},
|
||||
{ replaceRequest: false },
|
||||
)
|
||||
.mount(
|
||||
'/mcp/thinking/sse',
|
||||
async (request, env, ctx) => {
|
||||
return ThinkingMCP.serveSSE('/mcp/thinking/sse', { binding: 'THINKING_MCP' }).fetch(
|
||||
request,
|
||||
env,
|
||||
ctx,
|
||||
);
|
||||
},
|
||||
{ replaceRequest: false },
|
||||
)
|
||||
.mount(
|
||||
'/mcp',
|
||||
async (request, env, ctx) => {
|
||||
@@ -841,4 +853,4 @@ export default class extends WorkerEntrypoint<typeof env> {
|
||||
}
|
||||
}
|
||||
|
||||
export { ZeroAgent, ZeroMCP, ZeroDB, ZeroDriver };
|
||||
export { ZeroAgent, ZeroMCP, ZeroDB, ZeroDriver, ThinkingMCP };
|
||||
|
||||
@@ -1065,7 +1065,7 @@ export class ZeroAgent extends AIChatAgent<typeof env> {
|
||||
private chatMessageAbortControllers: Map<string, AbortController> = new Map();
|
||||
|
||||
async registerZeroMCP() {
|
||||
await this.mcp.connect(env.VITE_PUBLIC_BACKEND_URL + '/sse?mcpId=zero-mcp', {
|
||||
await this.mcp.connect(env.VITE_PUBLIC_BACKEND_URL + '/sse', {
|
||||
transport: {
|
||||
authProvider: new DurableObjectOAuthClientProvider(
|
||||
this.ctx.storage,
|
||||
@@ -1077,7 +1077,7 @@ export class ZeroAgent extends AIChatAgent<typeof env> {
|
||||
}
|
||||
|
||||
async registerThinkingMCP() {
|
||||
await this.mcp.connect(env.VITE_PUBLIC_BACKEND_URL + '/sse?mcpId=thinking-mcp', {
|
||||
await this.mcp.connect(env.VITE_PUBLIC_BACKEND_URL + '/mcp/thinking/sse', {
|
||||
transport: {
|
||||
authProvider: new DurableObjectOAuthClientProvider(
|
||||
this.ctx.storage,
|
||||
@@ -1089,7 +1089,7 @@ export class ZeroAgent extends AIChatAgent<typeof env> {
|
||||
}
|
||||
|
||||
onStart(): void | Promise<void> {
|
||||
// this.registerThinkingMCP();
|
||||
this.registerThinkingMCP();
|
||||
}
|
||||
|
||||
private getDataStreamResponse(
|
||||
@@ -1104,11 +1104,11 @@ export class ZeroAgent extends AIChatAgent<typeof env> {
|
||||
const connectionId = this.name;
|
||||
const orchestrator = new ToolOrchestrator(dataStream, connectionId);
|
||||
|
||||
// const mcpTools = this.mcp.unstable_getAITools();
|
||||
const mcpTools = this.mcp.unstable_getAITools();
|
||||
|
||||
const rawTools = {
|
||||
...(await authTools(connectionId)),
|
||||
// ...mcpTools,
|
||||
...mcpTools,
|
||||
};
|
||||
|
||||
const tools = orchestrator.processTools(rawTools);
|
||||
|
||||
@@ -43,6 +43,10 @@
|
||||
"name": "ZERO_DRIVER",
|
||||
"class_name": "ZeroDriver",
|
||||
},
|
||||
{
|
||||
"name": "THINKING_MCP",
|
||||
"class_name": "ThinkingMCP",
|
||||
},
|
||||
],
|
||||
},
|
||||
"queues": {
|
||||
@@ -86,6 +90,10 @@
|
||||
"tag": "v5",
|
||||
"new_sqlite_classes": ["ZeroDriver"],
|
||||
},
|
||||
{
|
||||
"tag": "v6",
|
||||
"new_sqlite_classes": ["ThinkingMCP"],
|
||||
},
|
||||
],
|
||||
|
||||
"observability": {
|
||||
@@ -187,6 +195,10 @@
|
||||
"name": "ZERO_DRIVER",
|
||||
"class_name": "ZeroDriver",
|
||||
},
|
||||
{
|
||||
"name": "THINKING_MCP",
|
||||
"class_name": "ThinkingMCP",
|
||||
},
|
||||
],
|
||||
},
|
||||
"r2_buckets": [
|
||||
@@ -240,6 +252,10 @@
|
||||
"tag": "v6",
|
||||
"new_sqlite_classes": ["ZeroDriver"],
|
||||
},
|
||||
{
|
||||
"tag": "v7",
|
||||
"new_sqlite_classes": ["ThinkingMCP"],
|
||||
},
|
||||
],
|
||||
"observability": {
|
||||
"enabled": true,
|
||||
@@ -344,6 +360,10 @@
|
||||
"name": "ZERO_DRIVER",
|
||||
"class_name": "ZeroDriver",
|
||||
},
|
||||
{
|
||||
"name": "ZERO_THINKING_MCP",
|
||||
"class_name": "ZeroThinkingMCP",
|
||||
},
|
||||
],
|
||||
},
|
||||
"queues": {
|
||||
@@ -391,6 +411,10 @@
|
||||
"tag": "v6",
|
||||
"new_sqlite_classes": ["ZeroDriver"],
|
||||
},
|
||||
{
|
||||
"tag": "v7",
|
||||
"new_sqlite_classes": ["ZeroThinkingMCP"],
|
||||
},
|
||||
],
|
||||
"vars": {
|
||||
"NODE_ENV": "production",
|
||||
|
||||
Reference in New Issue
Block a user