Skip to content

Commit f3c5df8

Browse files
authored
handle invalid no arg in tool (#26)
1 parent f256159 commit f3c5df8

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

.mcp.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"mcpServers": {
33
"next-devtools": {
4-
"command": "npx",
5-
"args": ["."]
4+
"command": "node",
5+
"args": ["./dist/index.js"]
66
}
77
}
88
}

src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ListResourcesRequestSchema,
1515
ReadResourceRequestSchema,
1616
} from "@modelcontextprotocol/sdk/types.js"
17+
import { ZodError } from "zod"
1718
import { MCP_TOOLS } from "./mcp-tools/index.js"
1819
import { MCP_PROMPTS, PROMPT_HANDLERS } from "./mcp-prompts/index.js"
1920
import {
@@ -61,7 +62,27 @@ async function main() {
6162
}
6263

6364
try {
64-
const result = await tool.execute?.(args as any, {
65+
// Validate arguments against the tool's input schema
66+
let validatedArgs = args || {}
67+
if (tool.inputSchema && typeof (tool.inputSchema as any).parse === "function") {
68+
try {
69+
validatedArgs = (tool.inputSchema as any).parse(args || {})
70+
} catch (error) {
71+
// Format Zod validation errors for better user experience
72+
if (error instanceof ZodError) {
73+
const formattedErrors = (error as any).issues
74+
.map((err: any) => {
75+
const path = err.path.length > 0 ? `${err.path.join(".")}` : "arguments"
76+
return `${path}: ${err.message}`
77+
})
78+
.join("; ")
79+
throw new Error(`Invalid arguments - ${formattedErrors}`)
80+
}
81+
throw error
82+
}
83+
}
84+
85+
const result = await tool.execute?.(validatedArgs as any, {
6586
abortSignal: new AbortController().signal,
6687
messages: [],
6788
toolCallId: String(request.params._meta?.progressToken || Date.now()),

src/mcp-tools/nextjs-docs.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88

99
// Next.js Docs Tool
1010
const nextjsDocsInputSchema = z.object({
11-
query: z.string().describe("Search query to find relevant Next.js documentation sections"),
11+
query: z
12+
.string()
13+
.min(1, "Query parameter is required and must be a non-empty string")
14+
.describe("Search query to find relevant Next.js documentation sections"),
1215
category: z
1316
.enum(["all", "getting-started", "guides", "api-reference", "architecture", "community"])
1417
.optional()
@@ -136,6 +139,7 @@ Provides access to comprehensive Next.js guides, API references, and best practi
136139
category = "all",
137140
}: z.infer<typeof nextjsDocsInputSchema>): Promise<string> => {
138141
// Step 1: Search MCP resources first (Next.js 16 knowledge base)
142+
// Note: Arguments are now validated by Zod at the MCP server level before reaching here
139143
const mcpMatches = searchMcpResources(query)
140144

141145
if (mcpMatches.length > 0) {

0 commit comments

Comments
 (0)