@@ -19,6 +19,7 @@ import (
1919 "strings"
2020 "time"
2121
22+ "github.com/modelcontextprotocol/go-sdk/auth"
2223 "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
2324 "github.com/modelcontextprotocol/go-sdk/jsonrpc"
2425)
@@ -132,7 +133,8 @@ func handleReceive[S Session](ctx context.Context, session S, jreq *jsonrpc.Requ
132133 }
133134
134135 mh := session .receivingMethodHandler ().(MethodHandler )
135- req := info .newRequest (session , params )
136+ ti , _ := jreq .Meta .(* auth.TokenInfo )
137+ req := info .newRequest (session , params , ti )
136138 // mh might be user code, so ensure that it returns the right values for the jsonrpc2 protocol.
137139 res , err := mh (ctx , jreq .Method , req )
138140 if err != nil {
@@ -179,7 +181,7 @@ type methodInfo struct {
179181 // Unmarshal params from the wire into a Params struct.
180182 // Used on the receive side.
181183 unmarshalParams func (json.RawMessage ) (Params , error )
182- newRequest func (Session , Params ) Request
184+ newRequest func (Session , Params , * auth. TokenInfo ) Request
183185 // Run the code when a call to the method is received.
184186 // Used on the receive side.
185187 handleMethod methodHandler
@@ -214,7 +216,7 @@ const (
214216
215217func newClientMethodInfo [P paramsPtr [T ], R Result , T any ](d typedClientMethodHandler [P , R ], flags methodFlags ) methodInfo {
216218 mi := newMethodInfo [P , R ](flags )
217- mi .newRequest = func (s Session , p Params ) Request {
219+ mi .newRequest = func (s Session , p Params , _ * auth. TokenInfo ) Request {
218220 r := & ClientRequest [P ]{Session : s .(* ClientSession )}
219221 if p != nil {
220222 r .Params = p .(P )
@@ -229,19 +231,15 @@ func newClientMethodInfo[P paramsPtr[T], R Result, T any](d typedClientMethodHan
229231
230232func newServerMethodInfo [P paramsPtr [T ], R Result , T any ](d typedServerMethodHandler [P , R ], flags methodFlags ) methodInfo {
231233 mi := newMethodInfo [P , R ](flags )
232- mi .newRequest = func (s Session , p Params ) Request {
233- r := & ServerRequest [P ]{Session : s .(* ServerSession )}
234+ mi .newRequest = func (s Session , p Params , ti * auth. TokenInfo ) Request {
235+ r := & ServerRequest [P ]{Session : s .(* ServerSession ), TokenInfo : ti }
234236 if p != nil {
235237 r .Params = p .(P )
236238 }
237239 return r
238240 }
239241 mi .handleMethod = MethodHandler (func (ctx context.Context , _ string , req Request ) (Result , error ) {
240- rf := & ServerRequest [P ]{Session : req .GetSession ().(* ServerSession )}
241- if req .GetParams () != nil {
242- rf .Params = req .GetParams ().(P )
243- }
244- return d (ctx , rf )
242+ return d (ctx , req .(* ServerRequest [P ]))
245243 })
246244 return mi
247245}
@@ -395,8 +393,9 @@ type ClientRequest[P Params] struct {
395393
396394// A ServerRequest is a request to a server.
397395type ServerRequest [P Params ] struct {
398- Session * ServerSession
399- Params P
396+ Session * ServerSession
397+ Params P
398+ TokenInfo * auth.TokenInfo // bearer token info (e.g. from OAuth) if any
400399}
401400
402401func (* ClientRequest [P ]) isRequest () {}
0 commit comments