mcp: introduce Requests (#267)
Handlers and client/server methods take a single request argument combining the session and params. This slightly simplifies the signature for handlers, since there is no longer an often-ignored session argument. But more importantly, it opens the door to adding more information in requests, such as auth info and HTTP request headers. For #243.
This commit is contained in:
parent
e097918484
commit
6e03217c83
1 changed files with 517 additions and 380 deletions
|
|
@ -29,36 +29,35 @@ func Example_loggingMiddleware() {
|
|||
},
|
||||
}))
|
||||
|
||||
loggingMiddleware := func(next mcp.MethodHandler[*mcp.ServerSession]) mcp.MethodHandler[*mcp.ServerSession] {
|
||||
loggingMiddleware := func(next mcp.MethodHandler) mcp.MethodHandler {
|
||||
return func(
|
||||
ctx context.Context,
|
||||
session *mcp.ServerSession,
|
||||
method string,
|
||||
params mcp.Params,
|
||||
req mcp.Request,
|
||||
) (mcp.Result, error) {
|
||||
logger.Info("MCP method started",
|
||||
"method", method,
|
||||
"session_id", session.ID(),
|
||||
"has_params", params != nil,
|
||||
"session_id", req.GetSession().ID(),
|
||||
"has_params", req.GetParams() != nil,
|
||||
)
|
||||
|
||||
start := time.Now()
|
||||
|
||||
result, err := next(ctx, session, method, params)
|
||||
result, err := next(ctx, method, req)
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
if err != nil {
|
||||
logger.Error("MCP method failed",
|
||||
"method", method,
|
||||
"session_id", session.ID(),
|
||||
"session_id", req.GetSession().ID(),
|
||||
"duration_ms", duration.Milliseconds(),
|
||||
"err", err,
|
||||
)
|
||||
} else {
|
||||
logger.Info("MCP method completed",
|
||||
"method", method,
|
||||
"session_id", session.ID(),
|
||||
"session_id", req.GetSession().ID(),
|
||||
"duration_ms", duration.Milliseconds(),
|
||||
"has_result", result != nil,
|
||||
)
|
||||
|
|
@ -90,10 +89,9 @@ func Example_loggingMiddleware() {
|
|||
},
|
||||
func(
|
||||
ctx context.Context,
|
||||
ss *mcp.ServerSession,
|
||||
params *mcp.CallToolParamsFor[map[string]any],
|
||||
req *mcp.ServerRequest[*mcp.CallToolParamsFor[map[string]any]],
|
||||
) (*mcp.CallToolResultFor[any], error) {
|
||||
name, ok := params.Arguments["name"].(string)
|
||||
name, ok := req.Params.Arguments["name"].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("name parameter is required and must be a string")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue