-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathserver.go
More file actions
94 lines (81 loc) · 2.7 KB
/
Copy pathserver.go
File metadata and controls
94 lines (81 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package mcp
import (
"context"
"fmt"
"os"
mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/ankitpokhrel/jira-cli/internal/mcp/tools"
)
const (
// ServerName is the implementation name advertised over MCP.
ServerName = "jira-cli"
// ServerVersion is the MCP server version advertised to clients.
// Bumped independently of the jira-cli release version when the MCP
// surface changes in a backward-incompatible way.
ServerVersion = "0.1.0"
)
// NewServer constructs a configured *mcp.Server with all jira-cli tools
// registered. The caller is responsible for invoking server.Run with a
// transport.
func NewServer(d *tools.Deps) *mcpsdk.Server {
srv := mcpsdk.NewServer(&mcpsdk.Implementation{
Name: ServerName,
Version: ServerVersion,
}, nil)
registerTool(srv, "search_issues",
"Search Jira issues by JQL or simple filters. Defaults to the configured project.",
d, tools.SearchIssues)
registerTool(srv, "get_issue",
"Get full details of a Jira issue including description and recent comments.",
d, tools.GetIssue)
registerTool(srv, "create_issue",
"Create a new Jira issue in the given project.",
d, tools.CreateIssue)
registerTool(srv, "add_comment",
"Add a comment to a Jira issue.",
d, tools.AddComment)
registerTool(srv, "transition_issue",
"Transition a Jira issue to a new status by name (e.g. \"In Progress\", \"Done\").",
d, tools.TransitionIssue)
return srv
}
// registerTool adapts a tools.* handler (which takes Deps + Input and returns
// Output + error) onto the SDK's expected handler signature. It also recovers
// from panics in the handler body so a single bad call cannot kill the server
// mid-session, and converts both errors and panics into MCP tool errors that
// the LLM can read.
func registerTool[In, Out any](
srv *mcpsdk.Server,
name, description string,
d *tools.Deps,
fn func(context.Context, *tools.Deps, In) (Out, error),
) {
mcpsdk.AddTool(srv,
&mcpsdk.Tool{Name: name, Description: description},
func(ctx context.Context, _ *mcpsdk.CallToolRequest, in In) (result *mcpsdk.CallToolResult, out Out, err error) {
defer func() {
if r := recover(); r != nil {
var zero Out
fmt.Fprintf(os.Stderr, "mcp: panic in tool %q: %v\n", name, r)
result = &mcpsdk.CallToolResult{
IsError: true,
Content: []mcpsdk.Content{&mcpsdk.TextContent{
Text: fmt.Sprintf("internal error in tool %q: %v", name, r),
}},
}
out = zero
err = nil
}
}()
out, callErr := fn(ctx, d, in)
if callErr != nil {
var zero Out
return &mcpsdk.CallToolResult{
IsError: true,
Content: []mcpsdk.Content{&mcpsdk.TextContent{Text: callErr.Error()}},
}, zero, nil
}
return nil, out, nil
},
)
}