Skip to content

Flesh out MCP initialization lifecycle #6

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

Merged
merged 1 commit into from
Feb 14, 2025
Merged
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
23 changes: 21 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ func NewClient(transport transport.Transport) *Client {
}
}

// A bit loosey goosey, but it works for now.
// See: https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/
type InitializeRequestParams struct {
ProtocolVersion string `json:"protocolVersion"`
ClientInfo InitializeRequestClientInfo `json:"clientInfo"`
Capabilities map[string]interface{} `json:"capabilities"`
}

type InitializeRequestClientInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}

// Initialize connects to the server and retrieves its capabilities
func (c *Client) Initialize(ctx context.Context) (*InitializeResponse, error) {
func (c *Client) Initialize(ctx context.Context, params *InitializeRequestParams) (*InitializeResponse, error) {
if c.initialized {
return nil, errors.New("client already initialized")
}
Expand All @@ -37,7 +50,7 @@ func (c *Client) Initialize(ctx context.Context) (*InitializeResponse, error) {
}

// Make initialize request to server
response, err := c.protocol.Request(ctx, "initialize", map[string]interface{}{}, nil)
response, err := c.protocol.Request(ctx, "initialize", params, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize")
}
Expand All @@ -53,8 +66,14 @@ func (c *Client) Initialize(ctx context.Context) (*InitializeResponse, error) {
return nil, errors.Wrap(err, "failed to unmarshal initialize response")
}

err = c.protocol.Notification("notifications/initialized", nil)
if err != nil {
return nil, errors.Wrap(err, "failed to send initialized notification")
}

c.capabilities = &initResult.Capabilities
c.initialized = true

return &initResult, nil
}

Expand Down
18 changes: 17 additions & 1 deletion examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import (
"github.com/rvoh-emccaleb/mcp-golang/transport/stdio"
)

const (
ProtocolVersion = "2024-11-05"
)

func main() {
// Start the server process
cmd := exec.Command("go", "run", "./server/main.go")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatalf("Failed to get stdin pipe: %v", err)
}

stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("Failed to get stdout pipe: %v", err)
Expand All @@ -25,12 +30,23 @@ func main() {
if err := cmd.Start(); err != nil {
log.Fatalf("Failed to start server: %v", err)
}

defer cmd.Process.Kill()

clientTransport := stdio.NewStdioServerTransportWithIO(stdout, stdin)
client := mcp_golang.NewClient(clientTransport)

if _, err := client.Initialize(context.Background()); err != nil {
if _, err := client.Initialize(
context.Background(),
&mcp_golang.InitializeRequestParams{
ClientInfo: mcp_golang.InitializeRequestClientInfo{
Name: "example-client",
Version: "0.1.0",
},
ProtocolVersion: ProtocolVersion,
Capabilities: nil,
},
); err != nil {
log.Fatalf("Failed to initialize client: %v", err)
}

Expand Down
16 changes: 15 additions & 1 deletion examples/http_example/auth_example_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/rvoh-emccaleb/mcp-golang/transport/http"
)

const (
ProtocolVersion = "2024-11-05"
)

func main() {
// Create an HTTP transport that connects to the server
transport := http.NewHTTPClientTransport("/mcp")
Expand All @@ -20,7 +24,17 @@ func main() {
client := mcp_golang.NewClient(transport)

// Initialize the client
if resp, err := client.Initialize(context.Background()); err != nil {
if resp, err := client.Initialize(
context.Background(),
&mcp_golang.InitializeRequestParams{
ClientInfo: mcp_golang.InitializeRequestClientInfo{
Name: "example-client",
Version: "0.1.0",
},
ProtocolVersion: ProtocolVersion,
Capabilities: nil,
},
); err != nil {
log.Fatalf("Failed to initialize client: %v", err)
} else {
log.Printf("Initialized client: %v", spew.Sdump(resp))
Expand Down
17 changes: 16 additions & 1 deletion examples/http_example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/rvoh-emccaleb/mcp-golang/transport/http"
)

const (
ProtocolVersion = "2024-11-05"
)

func main() {
// Create an HTTP transport that connects to the server
transport := http.NewHTTPClientTransport("/mcp")
Expand All @@ -19,8 +23,19 @@ func main() {
client := mcp_golang.NewClient(transport)

// Initialize the client
if resp, err := client.Initialize(context.Background()); err != nil {
if resp, err := client.Initialize(
context.Background(),
&mcp_golang.InitializeRequestParams{
ClientInfo: mcp_golang.InitializeRequestClientInfo{
Name: "example-client",
Version: "0.1.0",
},
ProtocolVersion: ProtocolVersion,
Capabilities: nil,
},
); err != nil {
log.Fatalf("Failed to initialize client: %v", err)

} else {
log.Printf("Initialized client: %v", spew.Sdump(resp))
}
Expand Down