Skip to content

refactor(mcp): add NewServerFrom constructor for dependency injection (for module extensibility)#754

Open
mfaizanse wants to merge 4 commits intocontainers:mainfrom
mfaizanse:feature/mcp-server-public3
Open

refactor(mcp): add NewServerFrom constructor for dependency injection (for module extensibility)#754
mfaizanse wants to merge 4 commits intocontainers:mainfrom
mfaizanse:feature/mcp-server-public3

Conversation

@mfaizanse
Copy link
Copy Markdown

@mfaizanse mfaizanse commented Feb 9, 2026

Summary

This PR refactors the MCP server construction to support dependency injection, enabling external Go modules to customize the server when using kubernetes-mcp-server as a module dependency.

Changes

New Constructor: NewServerFrom

Added a new exported constructor that accepts pre-configured components:

func NewServerFrom(
    configuration Configuration,
    mcpServer *mcp.Server,
    targetProvider internalk8s.Provider,
    metrics *metrics.Metrics,
) (*Server, error)

Use Cases

External packages can now:

  • Provide a custom mcp.Server with different options or additional middleware.
  • Inject custom metrics implementations.
  • Control middleware order and composition. For example, passing custom metadata from ToolsParams to the Provider etc.
  • Reuse core server logic with different configurations

Example usage

import "github.com/containers/kubernetes-mcp-server/pkg/mcp"

// Create custom MCP server with your middleware
mcpServer := mcp.NewServer(impl, opts)
mcpServer.AddReceivingMiddleware(myCustomMiddleware)

// Use NewServerFrom with your custom components
mcpServer, err := mcp.NewServerFrom(config, mcpServer, provider, myMetrics)

if m.StaticConfig.Port != "" {
    ctx := context.Background()
    return internalhttp.Serve(ctx, mcpServer, m.StaticConfig, oidcProvider, httpClient)
}

Signed-off-by: Muhammad Faizan <mfaizan.se@gmail.com>
@Cali0707 Cali0707 requested a review from manusa February 10, 2026 15:23
Signed-off-by: Muhammad Faizan <mfaizan.se@gmail.com>
Signed-off-by: Muhammad Faizan <mfaizan.se@gmail.com>
@matzew
Copy link
Copy Markdown
Collaborator

matzew commented Feb 24, 2026

Hey 👋

thanks for the PR, mind to update it ?

Signed-off-by: Muhammad Faizan <mfaizan.se@gmail.com>
@mfaizanse
Copy link
Copy Markdown
Author

Hey 👋

thanks for the PR, mind to update it ?

@matzew I have resolved the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants