Skip to content

feat: add request header #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func (c *Client) sendRequest(
Method: method,
Params: params,
}

response, err := c.transport.SendRequest(ctx, request)
if err != nil {
return nil, fmt.Errorf("transport error: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions client/transport/inprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *InProcessTransport) SendRequest(ctx context.Context, request JSONRPCReq
}
requestBytes = append(requestBytes, '\n')

respMessage := c.server.HandleMessage(ctx, requestBytes)
respMessage := c.server.HandleMessage(ctx, map[string]string{}, requestBytes)
respByte, err := json.Marshal(respMessage)
if err != nil {
return nil, fmt.Errorf("failed to marshal response message: %w", err)
Expand All @@ -54,7 +54,7 @@ func (c *InProcessTransport) SendNotification(ctx context.Context, notification
return fmt.Errorf("failed to marshal notification: %w", err)
}
notificationBytes = append(notificationBytes, '\n')
c.server.HandleMessage(ctx, notificationBytes)
c.server.HandleMessage(ctx, map[string]string{}, notificationBytes)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions mcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ func (m *Meta) UnmarshalJSON(data []byte) error {
}

type Request struct {
Method string `json:"method"`
Params RequestParams `json:"params,omitempty"`
Header map[string]string `json:"header"`
Method string `json:"method"`
Params RequestParams `json:"params,omitempty"`
}

type RequestParams struct {
Expand Down
2 changes: 2 additions & 0 deletions server/internal/gen/request_handler.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// HandleMessage processes an incoming JSON-RPC message and returns an appropriate response
func (s *MCPServer) HandleMessage(
ctx context.Context,
header map[string]string,
message json.RawMessage,
) mcp.JSONRPCMessage {
// Add server to context
Expand Down Expand Up @@ -90,6 +91,7 @@ func (s *MCPServer) HandleMessage(
err: &UnparsableMessageError{message: message, err: unmarshalErr, method: baseMessage.Method},
}
} else {
request.Header = header
s.hooks.before{{.HookName}}(ctx, baseMessage.ID, &request)
result, err = s.{{.HandlerFunc}}(ctx, baseMessage.ID, request)
}
Expand Down
11 changes: 11 additions & 0 deletions server/request_handler.go

This comment was marked as resolved.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions server/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func TestMCPServer_RemoveResource(t *testing.T) {
)

// First, verify we have two resources
response := server.HandleMessage(context.Background(), []byte(`{
header := map[string]string{"Authorization": "Bearer test"}
response := server.HandleMessage(context.Background(), header, []byte(`{
"jsonrpc": "2.0",
"id": 1,
"method": "resources/list"
Expand Down Expand Up @@ -205,9 +206,9 @@ func TestMCPServer_RemoveResource(t *testing.T) {
"1.0.0",
WithResourceCapabilities(true, true),
)

header := map[string]string{"Authorization": "Bearer test"}
// Initialize the server
_ = server.HandleMessage(ctx, []byte(`{
_ = server.HandleMessage(ctx, header, []byte(`{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize"
Expand Down Expand Up @@ -244,7 +245,7 @@ func TestMCPServer_RemoveResource(t *testing.T) {
"id": 1,
"method": "resources/list"
}`
resourcesList := server.HandleMessage(ctx, []byte(listMessage))
resourcesList := server.HandleMessage(ctx, header, []byte(listMessage))

// Validate the results
tt.validate(t, notifications, resourcesList)
Expand Down
Loading