mcp: update return meta in DiscoverResult (SEP-2575) (#1097)
This PR implements the latest changes introduced by https://github.com/modelcontextprotocol/modelcontextprotocol/pull/3002
This commit is contained in:
parent
fa548d7401
commit
5cb8339210
1 changed files with 210 additions and 68 deletions
|
|
@ -888,17 +888,21 @@ func (s *Server) discover(_ context.Context, req *ServerRequest[*DiscoverParams]
|
|||
if versions == nil {
|
||||
versions = slices.Clone(supportedProtocolVersions)
|
||||
}
|
||||
// Read the request-scoped identity/capabilities before acquiring the
|
||||
// session lock: these accessors may fall back to Session.InitializeParams
|
||||
// (which also locks Session.mu), so calling them from inside updateState
|
||||
// would self-deadlock.
|
||||
init := &InitializeParams{
|
||||
ProtocolVersion: req.ProtocolVersion(),
|
||||
Capabilities: req.ClientCapabilities(),
|
||||
ClientInfo: req.ClientInfo(),
|
||||
}
|
||||
req.Session.updateState(func(state *ServerSessionState) {
|
||||
state.InitializeParams = &InitializeParams{
|
||||
ProtocolVersion: req.ProtocolVersion(),
|
||||
Capabilities: req.ClientCapabilities(),
|
||||
ClientInfo: req.ClientInfo(),
|
||||
}
|
||||
state.InitializeParams = init
|
||||
})
|
||||
res := &DiscoverResult{
|
||||
SupportedVersions: versions,
|
||||
Capabilities: s.capabilities(),
|
||||
ServerInfo: s.impl,
|
||||
Instructions: s.opts.Instructions,
|
||||
}
|
||||
res.setDefaultCacheableValues()
|
||||
|
|
@ -1924,10 +1928,32 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
|
|||
}
|
||||
if validatedMeta.usesNewProtocol {
|
||||
setCompleteResultType(res)
|
||||
annotateServerInfo(res, ss.server.impl)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// annotateServerInfo sets [MetaKeyServerInfo] on the result's `_meta` unless
|
||||
// it is already present. Per SEP-2575, servers should identify themselves in
|
||||
// each result's `_meta`.
|
||||
func annotateServerInfo(res Result, impl *Implementation) {
|
||||
if res == nil || impl == nil {
|
||||
return
|
||||
}
|
||||
if _, isEmpty := res.(*emptyResult); isEmpty {
|
||||
return
|
||||
}
|
||||
m := res.GetMeta()
|
||||
if m == nil {
|
||||
m = map[string]any{}
|
||||
}
|
||||
if _, ok := m[MetaKeyServerInfo]; ok {
|
||||
return
|
||||
}
|
||||
m[MetaKeyServerInfo] = impl
|
||||
res.SetMeta(m)
|
||||
}
|
||||
|
||||
// InitializeParams returns the InitializeParams provided during the client's
|
||||
// initial connection.
|
||||
func (ss *ServerSession) InitializeParams() *InitializeParams {
|
||||
|
|
|
|||
Loading…
Reference in a new issue