# Directory Structure
```
├── .gitignore
├── bin
│ └── deep-reasoning-mcp.js
├── biome.json
├── LICENSE
├── package.json
├── pnpm-lock.yaml
├── README.md
├── rslib.config.ts
├── src
│ ├── index.ts
│ ├── prompt.ts
│ └── sse.ts
└── tsconfig.json
```
# Files
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
```
1 | # Local
2 | .DS_Store
3 | *.local
4 | *.log*
5 |
6 | # Dist
7 | node_modules
8 | dist/
9 |
10 | # IDE
11 | .vscode/*
12 | !.vscode/extensions.json
13 | .idea
14 |
```
--------------------------------------------------------------------------------
/bin/deep-reasoning-mcp.js:
--------------------------------------------------------------------------------
```javascript
1 | #!/usr/bin/env node
2 |
3 | async function main() {
4 | const { run } = await import("../dist/index.js");
5 |
6 | try {
7 | run();
8 | } catch (err) {
9 | console.error(err);
10 | }
11 | }
12 |
13 | main();
14 |
```
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
```json
1 | {
2 | "compilerOptions": {
3 | "lib": ["ES2021"],
4 | "module": "ESNext",
5 | "noEmit": true,
6 | "strict": true,
7 | "skipLibCheck": true,
8 | "isolatedModules": true,
9 | "resolveJsonModule": true,
10 | "moduleResolution": "bundler",
11 | "useDefineForClassFields": true,
12 | "allowImportingTsExtensions": true
13 | },
14 | "include": ["src"]
15 | }
16 |
```
--------------------------------------------------------------------------------
/biome.json:
--------------------------------------------------------------------------------
```json
1 | {
2 | "$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
3 | "organizeImports": {
4 | "enabled": true
5 | },
6 | "vcs": {
7 | "enabled": true,
8 | "clientKind": "git",
9 | "useIgnoreFile": true
10 | },
11 | "formatter": {
12 | "indentStyle": "space"
13 | },
14 | "javascript": {
15 | "formatter": {
16 | "quoteStyle": "single"
17 | }
18 | },
19 | "css": {
20 | "parser": {
21 | "cssModules": true
22 | }
23 | },
24 | "linter": {
25 | "enabled": true,
26 | "rules": {
27 | "recommended": true
28 | }
29 | }
30 | }
31 |
```
--------------------------------------------------------------------------------
/rslib.config.ts:
--------------------------------------------------------------------------------
```typescript
1 | import { defineConfig } from "@rslib/core";
2 | import pkg from "./package.json";
3 |
4 | export default defineConfig({
5 | lib: [
6 | {
7 | format: "esm",
8 | syntax: "es2021",
9 | dts: true,
10 | },
11 | {
12 | format: "cjs",
13 | syntax: "es2021",
14 | },
15 | ],
16 | source: {
17 | entry: {
18 | index: "./src/index.ts",
19 | sse: "./src/sse.ts",
20 | },
21 | define: {
22 | "process.env.APP_NAME": JSON.stringify(pkg.name),
23 | "process.env.APP_VERSION": JSON.stringify(pkg.version),
24 | },
25 | },
26 | });
27 |
```
--------------------------------------------------------------------------------
/src/sse.ts:
--------------------------------------------------------------------------------
```typescript
1 | import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
2 | import express from "express";
3 | import { server } from "./index.ts";
4 |
5 | const app = express();
6 |
7 | let transport: SSEServerTransport;
8 |
9 | app.get("/sse", async (req, res) => {
10 | console.log("Received connection");
11 | transport = new SSEServerTransport("/message", res);
12 | await server.connect(transport);
13 | });
14 |
15 | app.post("/message", async (req, res) => {
16 | console.log("Received message");
17 | console.log(req);
18 |
19 | await transport.handlePostMessage(req, res);
20 | });
21 |
22 | const PORT = process.env.PORT || 3001;
23 | app.listen(PORT, () => {
24 | console.log(`Server is running on port ${PORT}`);
25 | });
26 |
```
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
```json
1 | {
2 | "name": "deep-reasoning-mcp",
3 | "version": "0.1.1",
4 | "description": "Deep reasoning MCP Server",
5 | "author": {
6 | "name": "Lakphy",
7 | "email": "[email protected]"
8 | },
9 | "license": "MIT",
10 | "keywords": [
11 | "mcp",
12 | "ai",
13 | "reasoning",
14 | "model-context-protocol"
15 | ],
16 | "repository": {
17 | "type": "git",
18 | "url": "git+https://github.com/lakphy/deep-reasoning-mcp.git"
19 | },
20 | "homepage": "https://github.com/lakphy/deep-reasoning-mcp#readme",
21 | "bugs": {
22 | "url": "https://github.com/lakphy/deep-reasoning-mcp/issues"
23 | },
24 | "type": "module",
25 | "exports": {
26 | ".": {
27 | "types": "./dist/index.d.ts",
28 | "import": "./dist/index.js",
29 | "require": "./dist/index.cjs"
30 | }
31 | },
32 | "main": "./dist/index.cjs",
33 | "module": "./dist/index.js",
34 | "types": "./dist/index.d.ts",
35 | "files": [
36 | "dist",
37 | "bin"
38 | ],
39 | "bin": {
40 | "deep-reasoning-mcp": "./bin/deep-reasoning-mcp.js"
41 | },
42 | "scripts": {
43 | "build": "rslib build",
44 | "dev": "rslib build --watch",
45 | "start": "nodemon dist/index.js",
46 | "start:sse": "nodemon dist/sse.js",
47 | "debug": "npx @modelcontextprotocol/inspector node bin/deep-reasoning-mcp.js",
48 | "check": "biome check --write",
49 | "format": "biome format --write"
50 | },
51 | "devDependencies": {
52 | "@biomejs/biome": "^1.9.4",
53 | "@modelcontextprotocol/inspector": "^0.4.1",
54 | "@rslib/core": "^0.4.1",
55 | "@types/node": "^22.13.4",
56 | "nodemon": "^3.0.2",
57 | "typescript": "^5.7.3",
58 | "@types/express": "^5.0.0"
59 | },
60 | "dependencies": {
61 | "@modelcontextprotocol/sdk": "^1.5.0",
62 | "@openrouter/ai-sdk-provider": "^0.2.1",
63 | "ai": "^4.1.40",
64 | "zod": "^3.24.2",
65 | "express": "^4.21.2"
66 | }
67 | }
68 |
```
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
```typescript
1 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2 | import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3 | import { z } from "zod";
4 | import { createOpenRouter } from "@openrouter/ai-sdk-provider";
5 | import { streamText } from "ai";
6 | import {
7 | getReasoningModelProcessTarget,
8 | getReasoningModelSystem,
9 | getResponsePrompt,
10 | } from "./prompt";
11 |
12 | const apiKey =
13 | process.argv.find((arg) => arg.startsWith("--apiKey="))?.split("=")[1] ||
14 | process.env.OPEN_ROUTER_API_KEY;
15 |
16 | const model =
17 | process.argv.find((arg) => arg.startsWith("--model="))?.split("=")[1] ||
18 | "deepseek/deepseek-r1-distill-qwen-32b";
19 |
20 | if (!apiKey) {
21 | throw new Error("API key is required");
22 | }
23 |
24 | const openrouter = createOpenRouter({
25 | apiKey: apiKey,
26 | });
27 |
28 | export const server = new McpServer({
29 | name: process.env.APP_NAME || "deep-reasoning-server",
30 | version: process.env.APP_VERSION || "0.1.0",
31 | });
32 |
33 | server.tool(
34 | "deep-reasoning",
35 | "Deep inference analysis of any input. This is a powerful deep reasoning tool that helps in analyzing issues, user intent, code logic, etc. This tool should be prioritized for analysis before working on any problem. If you can connect to the internet, you can search for more information as context before reasoning.",
36 | {
37 | statement: z
38 | .string()
39 | .describe(
40 | "Content to be analyzed by inference, which can be questions, statements, user intent, etc."
41 | ),
42 | context: z
43 | .string()
44 | .optional()
45 | .describe(
46 | "Important contextual information for enhancing the accuracy and completeness of reasoning. It is highly recommended to provide relevant contextual information, historical data, constraints, etc. for a more comprehensive analysis. Even in cases of uncertainty, it is recommended to provide context that may be relevant"
47 | ),
48 | },
49 | async ({ statement, context }) => {
50 | try {
51 | const startTime = Date.now();
52 | const result = streamText({
53 | model: openrouter(model, {
54 | includeReasoning: true,
55 | }),
56 | system: getReasoningModelSystem(),
57 | messages: [
58 | {
59 | role: "user",
60 | content: getReasoningModelProcessTarget({ statement, context }),
61 | },
62 | ],
63 | });
64 |
65 | for await (const textPart of result.textStream) continue;
66 |
67 | const resultText = await result.text;
68 | const resultReasoning = await result.reasoning;
69 |
70 | const duration = (Date.now() - startTime) / 1000;
71 |
72 | return {
73 | content: [
74 | {
75 | type: "text",
76 | text: getResponsePrompt({
77 | text: resultText,
78 | reasoning: resultReasoning || "",
79 | duration,
80 | }),
81 | },
82 | ],
83 | };
84 | } catch (error) {
85 | return {
86 | content: [{ type: "text", text: JSON.stringify({ error }) }],
87 | };
88 | }
89 | }
90 | );
91 |
92 | export async function run() {
93 | const transport = new StdioServerTransport();
94 | await server.connect(transport);
95 | }
96 |
```
--------------------------------------------------------------------------------
/src/prompt.ts:
--------------------------------------------------------------------------------
```typescript
1 | export const getResponsePrompt = ({
2 | text,
3 | reasoning,
4 | duration,
5 | }: {
6 | text: string;
7 | reasoning: string;
8 | duration: number;
9 | }) => `
10 | <reasoning>
11 |
12 | ${reasoning ? ["<idea>", reasoning, "</idea>"].join("\n") : ""}
13 |
14 | ${text ? ["<text>", text, "</text>"].join("\n") : ""}
15 |
16 | <rules>
17 |
18 | Please analyze the above carefully:
19 | 1. the <text> section contains the preliminary conclusions of the reasoning tool
20 | 2. the <idea> section (if present) shows the thinking process of the reasoning
21 |
22 | Your task is to:
23 | 1. understand and synthesize the information in both sections
24 | 2. provide an optimized answer to the requirement:
25 | - More concise: remove redundant information and emphasize core points
26 | - More accurate: correcting possible errors and ensuring the accuracy of the information
27 | - More complete: add context or details as necessary.
28 | 3. point out and correct any logical gaps in reasoning, if you find them
29 |
30 | Please output the final answer in clear, professional language.
31 |
32 | </rules>
33 |
34 | <others>
35 |
36 | Reasoning time: ${duration} seconds
37 | Telling users the reasoning time improves user experience and professionalism.
38 |
39 | </others>
40 |
41 | <reasoning>
42 | `;
43 |
44 | export const getReasoningModelSystem = () => `
45 | <task>
46 |
47 | You are a professional reasoning and analysis engine that specializes in in-depth, systematic analysis of all types of problems. Your main responsibility is to perform a thorough inference analysis of any user-provided content (questions, statements, intentions, etc.) to help understand the nature of the problem and draw reasonable conclusions.
48 |
49 | </task>
50 |
51 | <reasoning_framework>
52 |
53 | 1. Deep understanding
54 | - Accurately grasp the core issue and key messages
55 | - Identify the nature and potential impact of the issue
56 | - Analyze the background and context of the issue
57 | - Consider multiple dimensions of the issue
58 |
59 | 2. Systems analysis
60 | - Decompose complex problems into manageable sub-problems
61 | - Establish logical linkages between problems
62 | - Identify key variables and influences
63 | - Consider short- and long-term impacts
64 |
65 | 3. Multidimensional reasoning
66 | - Logical reasoning: using deductive and inductive methods
67 | - Causal reasoning: analyzing chains of cause and effect
68 | - Analogical reasoning: drawing on similar experiences
69 | - Systematic reasoning: considering wholes and correlations
70 | - Critical thinking: questioning and testing assumptions
71 |
72 | 4. Conclusion generation
73 | - Synthesizing results from multiple perspectives
74 | - Weighing the pros and cons of different options
75 | - Provide specific supporting arguments
76 | - Explain conditions and limitations of the conclusions
77 |
78 | 5. Output optimization
79 | - Ensure the accuracy and reliability of the conclusions
80 | - Maintain clarity and conciseness of presentation
81 | - Highlight key findings and recommendations
82 | - Provide actionable insights
83 |
84 | </reasoning_framework>
85 |
86 | <guidelines>
87 |
88 | - Maintains an objective and neutral position at all times
89 | - Utilize a rigorous logical reasoning process
90 | - Considers multiple possible perspectives and scenarios
91 | - Clearly identifies uncertainties and risks
92 | - Provides concrete examples and supporting arguments
93 | - Utilize contextual information
94 | - Ensure that conclusions have real value
95 | - Avoid oversimplifying complex issues
96 |
97 | </guidelines>
98 | `;
99 |
100 | export const getReasoningModelProcessTarget = ({
101 | statement,
102 | context,
103 | }: {
104 | statement: string;
105 | context?: string;
106 | }) => `
107 | <input>
108 | ${statement}
109 | </input>
110 |
111 | ${
112 | context
113 | ? `<context>
114 | ${context}
115 | </context>`
116 | : ""
117 | }
118 |
119 | <rules>
120 |
121 | Please analyze the inputs by in-depth and systematic reasoning. This analytical process is critical and will directly affect the quality of subsequent decisions and actions. Please follow the steps outlined below:
122 |
123 | 1. Comprehensive understanding
124 | - Carefully analyze all aspects of the problem
125 | - Identify key messages and core elements
126 | - Consider the background and context of the issue
127 |
128 | 2. Systematic reasoning
129 | - Utilizes a variety of reasoning methods
130 | - Establish a clear chain of logic
131 | - Consider multiple possible perspectives
132 | - Analyze potential impacts and risks
133 |
134 | 3. Conclusion generation
135 | - Provide clear and precise conclusions
136 | - Provide clear and precise conclusions
137 | - Describe conditions and limitations of applicability
138 | - Highlight key findings and recommendations
139 |
140 | Output Requirements:
141 | 1. the reasoning process should be systematic and rigorous
142 | 2. conclusions should be accurate, practical and concise
143 | 3. clearly identify uncertainties, but do not require questions
144 | 4. ensure that the output is practically informative
145 |
146 | </rules>
147 | `;
148 |
```