Skip to content

Commit 3d86fd7

Browse files
committed
feat: allow overriding flags at a tool level
1 parent 106faeb commit 3d86fd7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

command.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type Command struct {
6464
// Tool and Resource are mutually exclusive.
6565
Tool string
6666

67+
// ToolFlags is a set of flags to automatically set for a given MCP command.
68+
ToolFlags []string
69+
6770
// Resource is the URI of the MCP resource this command provides.
6871
// If set, the command can be accessed via MCP as a resource.
6972
// Tool and Resource are mutually exclusive.

mcp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ type MCPServer struct {
163163
stderr io.Writer
164164
cmdFinder CommandFinder
165165
toolCmds map[string]*Command
166+
toolFlags map[string][]string // overrides flags for given commands
166167
resourceCmds map[string]*Command
167168
resourceTemplates map[string]*Command // Maps URI templates to commands
168169
initialized bool // Track if the server has been initialized
@@ -203,6 +204,7 @@ func NewMCPServer(rootCmd *Command, stdin io.Reader, stdout, stderr io.Writer) *
203204
stderr: stderr,
204205
cmdFinder: DefaultCommandFinder,
205206
toolCmds: make(map[string]*Command),
207+
toolFlags: make(map[string][]string),
206208
resourceCmds: make(map[string]*Command),
207209
resourceTemplates: make(map[string]*Command),
208210
protocolVersion: "2025-03-26", // Default to latest version
@@ -212,6 +214,9 @@ func NewMCPServer(rootCmd *Command, stdin io.Reader, stdout, stderr io.Writer) *
212214
rootCmd.Walk(func(cmd *Command) {
213215
if cmd.Tool != "" {
214216
server.toolCmds[cmd.Tool] = cmd
217+
if len(cmd.ToolFlags) > 0 {
218+
server.toolFlags[cmd.Tool] = cmd.ToolFlags[:]
219+
}
215220
}
216221
if cmd.Resource != "" {
217222
if strings.Contains(cmd.Resource, "{") && strings.Contains(cmd.Resource, "}") {
@@ -502,6 +507,13 @@ func (s *MCPServer) handleCallTool(req JSONRPC2Request) {
502507
}
503508
}
504509

510+
// Finally, add any overridden flags at the tool level.
511+
if toolFlags, ok := s.toolFlags[params.Name]; ok {
512+
for _, flag := range toolFlags {
513+
cmdArgs = append(cmdArgs, flag)
514+
}
515+
}
516+
505517
inv.Args = cmdArgs
506518

507519
// Run the command

0 commit comments

Comments
 (0)