1
1
// src/index.ts
2
2
import { Server } from "@modelcontextprotocol/sdk/server/index.js" ;
3
3
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
4
- import { ListToolsRequestSchema , CallToolRequestSchema , CallToolRequest , InitializeRequest } from "@modelcontextprotocol/sdk/types.js" ; // Added InitializeRequest
4
+ import {
5
+ ListToolsRequestSchema ,
6
+ CallToolRequestSchema ,
7
+ CallToolRequest ,
8
+ InitializeRequest ,
9
+ InitializeRequestSchema , // <-- Import InitializeRequestSchema
10
+ ToolDefinition // <-- Import ToolDefinition
11
+ } from "@modelcontextprotocol/sdk/types.js" ;
5
12
import { vibeCheckTool } from "./tools/vibeCheck.js" ;
6
13
import { vibeDistillTool } from "./tools/vibeDistill.js" ;
7
14
import { vibeLearnTool , VibeLearnOutput } from "./tools/vibeLearn.js" ;
8
15
import { MistakeEntry } from "./utils/storage.js" ;
9
- import { initializeGemini } from "./utils/gemini.js" ; // Import gemini initializer
16
+ import { initializeGemini } from "./utils/gemini.js" ;
10
17
11
- console . error ( "MCP Server: Script starting..." ) ; // Log to stderr
18
+ console . error ( "MCP Server: Script starting..." ) ;
12
19
13
20
// --- Explicitly Initialize Gemini ---
14
21
try {
15
- const apiKey = process . env . GEMINI_API_KEY ; // Read from env var
22
+ const apiKey = process . env . GEMINI_API_KEY ;
16
23
if ( ! apiKey ) {
17
- console . error ( "MCP Server: ERROR - GEMINI_API_KEY environment variable not found!" ) ;
18
- // Optionally exit if the key is absolutely required at startup
19
- // process.exit(1);
24
+ console . error ( "MCP Server: WARNING - GEMINI_API_KEY environment variable not found!" ) ;
25
+ // Continue without Gemini if key is missing, tools might fail later
20
26
} else {
21
27
initializeGemini ( apiKey ) ;
22
- console . error ( "MCP Server: Gemini client potentially initialized (check gemini.ts logs) ." ) ;
28
+ console . error ( "MCP Server: Gemini client potentially initialized." ) ;
23
29
}
24
30
} catch ( err : any ) {
25
31
console . error ( "MCP Server: ERROR initializing Gemini client -" , err ) ;
26
- // Optionally exit
27
- // process.exit(1);
32
+ // Continue anyway, tools using it will fail
28
33
}
29
34
// ------------------------------------
30
35
31
-
32
36
// Define the type for the 'cat' parameter explicitly
33
37
type CategorySummaryItem = {
34
38
category : string ;
@@ -44,47 +48,90 @@ const server = new Server({
44
48
} ) ;
45
49
console . error ( "MCP Server: Server instance created." ) ;
46
50
47
- // --- Add an explicit Initialize handler for logging ---
48
- server . setRequestHandler ( "initialize" , async ( request : InitializeRequest ) => {
49
- console . error ( `MCP Server: Received initialize request: ID=${ request . id } ` ) ;
50
- // Basic response - the SDK might override parts of this, but it confirms handling
51
+ // --- Add an explicit Initialize handler ---
52
+ // Use the Schema as the first argument, not the string "initialize"
53
+ server . setRequestHandler ( InitializeRequestSchema , async ( request : InitializeRequest ) => {
54
+ // Cannot reliably log request.id here based on type errors
55
+ console . error ( `MCP Server: Received initialize request` ) ;
51
56
const response = {
52
- protocolVersion : "2024-11-05" , // Use the version client sent or latest known
57
+ protocolVersion : request . params . protocolVersion , // Echo back client's version
53
58
serverInfo : {
54
59
name : "vibe-check-mcp" ,
55
60
version : "0.2.0" ,
56
61
} ,
57
- capabilities : {
58
- // Declare capabilities if needed, e.g., for notifications
59
- }
62
+ capabilities : { }
60
63
} ;
61
- console . error ( `MCP Server: Sending initialize response for ID= ${ request . id } ` ) ;
64
+ console . error ( `MCP Server: Sending initialize response` ) ;
62
65
return response ;
63
66
} ) ;
64
67
// -----------------------------------------------------
65
68
66
-
67
69
// Define tools using ListToolsRequestSchema handler
68
70
console . error ( "MCP Server: Setting ListTools request handler..." ) ;
69
71
server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
70
72
console . error ( "MCP Server: ListTools request received." ) ;
71
- // Your tool definitions...
72
- const tools = [ /* ... your tool definitions from previous code ... */ ] ;
73
+ // Explicitly type the tools array
74
+ const tools : ToolDefinition [ ] = [
75
+ {
76
+ name : "vibe_check" ,
77
+ description : "Metacognitive check for plan alignment and assumption testing." ,
78
+ inputSchema : {
79
+ type : "object" ,
80
+ properties : {
81
+ plan : { type : "string" } ,
82
+ userRequest : { type : "string" } ,
83
+ thinkingLog : { type : "string" } ,
84
+ availableTools : { type : "array" , items : { type : "string" } } ,
85
+ focusAreas : { type : "array" , items : { type : "string" } } ,
86
+ sessionId : { type : "string" } ,
87
+ previousAdvice : { type : "string" } ,
88
+ phase : { type : "string" , enum : [ "planning" , "implementation" , "review" ] } ,
89
+ confidence : { type : "number" }
90
+ } ,
91
+ required : [ "plan" , "userRequest" ]
92
+ }
93
+ } ,
94
+ {
95
+ name : "vibe_distill" ,
96
+ description : "Distills a plan to its essential core." ,
97
+ inputSchema : {
98
+ type : "object" ,
99
+ properties : {
100
+ plan : { type : "string" } ,
101
+ userRequest : { type : "string" } ,
102
+ sessionId : { type : "string" }
103
+ } ,
104
+ required : [ "plan" , "userRequest" ]
105
+ }
106
+ } ,
107
+ {
108
+ name : "vibe_learn" ,
109
+ description : "Logs mistake patterns for future improvement." ,
110
+ inputSchema : {
111
+ type : "object" ,
112
+ properties : {
113
+ mistake : { type : "string" } ,
114
+ category : { type : "string" } ,
115
+ solution : { type : "string" } ,
116
+ sessionId : { type : "string" }
117
+ } ,
118
+ required : [ "mistake" , "category" , "solution" ]
119
+ }
120
+ }
121
+ ] ;
73
122
console . error ( "MCP Server: Returning tool list." ) ;
74
- return { tools } ;
123
+ return { tools } ; // Type 'tools' variable here
75
124
} ) ;
76
125
console . error ( "MCP Server: ListTools request handler set." ) ;
77
126
78
-
79
127
// Handle tool calls using CallToolRequestSchema handler
80
128
console . error ( "MCP Server: Setting CallTool request handler..." ) ;
81
129
server . setRequestHandler ( CallToolRequestSchema , async ( request : CallToolRequest ) => {
82
130
const toolName = request . params . name ;
83
131
const args = request . params . arguments ?? { } ;
84
132
85
- // Log received request details to stderr
86
- console . error ( `MCP Server: CallToolRequest received for tool: ${ toolName } with ID: ${ request . id } ` ) ;
87
- // console.error(`MCP Server: Args: ${JSON.stringify(args)}`); // Be careful logging args if sensitive
133
+ // Cannot reliably log request.id here based on type errors
134
+ console . error ( `MCP Server: CallToolRequest received for tool: ${ toolName } ` ) ;
88
135
89
136
try {
90
137
let result : any ;
@@ -94,7 +141,15 @@ server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest)
94
141
result = await vibeCheckTool ( args as any ) ;
95
142
console . error ( `MCP Server: Tool ${ toolName } executed successfully.` ) ;
96
143
return { content : [ { type : "text" , text : result . questions + ( result . patternAlert ? `\n\n**Pattern Alert:** ${ result . patternAlert } ` : "" ) } ] } ;
97
- // ... other cases ...
144
+ case 'vibe_distill' :
145
+ result = await vibeDistillTool ( args as any ) ;
146
+ console . error ( `MCP Server: Tool ${ toolName } executed successfully.` ) ;
147
+ return { content : [ { type : "markdown" , markdown : result . distilledPlan + `\n\n**Rationale:** ${ result . rationale } ` } ] } ;
148
+ case 'vibe_learn' :
149
+ result = await vibeLearnTool ( args as any ) as VibeLearnOutput ;
150
+ console . error ( `MCP Server: Tool ${ toolName } executed successfully.` ) ;
151
+ const summary = result . topCategories . map ( ( cat : CategorySummaryItem ) => `- ${ cat . category } (${ cat . count } )` ) . join ( '\n' ) ;
152
+ return { content : [ { type : "text" , text : `✅ Pattern logged. Tally for category: ${ result . currentTally } .\nTop Categories:\n${ summary } ` } ] } ;
98
153
default :
99
154
console . error ( `MCP Server: Tool '${ toolName } ' not found.` ) ;
100
155
throw new Error ( `Tool '${ toolName } ' not found.` ) ;
@@ -111,7 +166,6 @@ server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest)
111
166
} ) ;
112
167
console . error ( "MCP Server: CallTool request handler set." ) ;
113
168
114
-
115
169
// Create the transport instance
116
170
console . error ( "MCP Server: Creating StdioServerTransport..." ) ;
117
171
const transport = new StdioServerTransport ( ) ;
@@ -122,10 +176,8 @@ console.error("MCP Server: Connecting server to transport...");
122
176
server . connect ( transport ) ;
123
177
console . error ( "MCP Server: Server connected to transport. Ready for messages." ) ;
124
178
125
- // Optional: Add listeners for transport errors/close events
126
- transport . onDidClose ( ( ) => {
179
+ // Use the corrected event name 'onclose'
180
+ transport . onclose ( ( ) => { // <-- Corrected event name
127
181
console . error ( "MCP Server: Transport closed event received." ) ;
128
182
} ) ;
129
- transport . onDidDispose ( ( ) => {
130
- console . error ( "MCP Server: Transport disposed event received." ) ;
131
- } ) ;
183
+ // Removed listener for non-existent onDidDispose
0 commit comments