@@ -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)
@@ -126,7 +127,8 @@ func handleReceive[S Session](ctx context.Context, session S, jreq *jsonrpc.Requ
126127 }
127128
128129 mh := session .receivingMethodHandler ()
129- req := info .newRequest (session , params )
130+ re , _ := jreq .Extra .(* RequestExtra )
131+ req := info .newRequest (session , params , re )
130132 // mh might be user code, so ensure that it returns the right values for the jsonrpc2 protocol.
131133 res , err := mh (ctx , jreq .Method , req )
132134 if err != nil {
@@ -173,7 +175,7 @@ type methodInfo struct {
173175 // Unmarshal params from the wire into a Params struct.
174176 // Used on the receive side.
175177 unmarshalParams func (json.RawMessage ) (Params , error )
176- newRequest func (Session , Params ) Request
178+ newRequest func (Session , Params , * RequestExtra ) Request
177179 // Run the code when a call to the method is received.
178180 // Used on the receive side.
179181 handleMethod MethodHandler
@@ -208,7 +210,7 @@ const (
208210
209211func newClientMethodInfo [P paramsPtr [T ], R Result , T any ](d typedClientMethodHandler [P , R ], flags methodFlags ) methodInfo {
210212 mi := newMethodInfo [P , R ](flags )
211- mi .newRequest = func (s Session , p Params ) Request {
213+ mi .newRequest = func (s Session , p Params , _ * RequestExtra ) Request {
212214 r := & ClientRequest [P ]{Session : s .(* ClientSession )}
213215 if p != nil {
214216 r .Params = p .(P )
@@ -223,19 +225,15 @@ func newClientMethodInfo[P paramsPtr[T], R Result, T any](d typedClientMethodHan
223225
224226func newServerMethodInfo [P paramsPtr [T ], R Result , T any ](d typedServerMethodHandler [P , R ], flags methodFlags ) methodInfo {
225227 mi := newMethodInfo [P , R ](flags )
226- mi .newRequest = func (s Session , p Params ) Request {
227- r := & ServerRequest [P ]{Session : s .(* ServerSession )}
228+ mi .newRequest = func (s Session , p Params , re * RequestExtra ) Request {
229+ r := & ServerRequest [P ]{Session : s .(* ServerSession ), Extra : re }
228230 if p != nil {
229231 r .Params = p .(P )
230232 }
231233 return r
232234 }
233235 mi .handleMethod = MethodHandler (func (ctx context.Context , _ string , req Request ) (Result , error ) {
234- rf := & ServerRequest [P ]{Session : req .GetSession ().(* ServerSession )}
235- if req .GetParams () != nil {
236- rf .Params = req .GetParams ().(P )
237- }
238- return d (ctx , rf )
236+ return d (ctx , req .(* ServerRequest [P ]))
239237 })
240238 return mi
241239}
@@ -391,6 +389,13 @@ type ClientRequest[P Params] struct {
391389type ServerRequest [P Params ] struct {
392390 Session * ServerSession
393391 Params P
392+ Extra * RequestExtra
393+ }
394+
395+ // RequestExtra is extra information included in requests, typically from
396+ // the transport layer.
397+ type RequestExtra struct {
398+ TokenInfo * auth.TokenInfo // bearer token info (e.g. from OAuth) if any
394399}
395400
396401func (* ClientRequest [P ]) isRequest () {}
0 commit comments