|  | 
|  | 1 | +// Copyright 2025 Google LLC All Rights Reserved. | 
|  | 2 | +// | 
|  | 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +// you may not use this file except in compliance with the License. | 
|  | 5 | +// You may obtain a copy of the License at | 
|  | 6 | +// | 
|  | 7 | +//    http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +// | 
|  | 9 | +// Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +// distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +// See the License for the specific language governing permissions and | 
|  | 13 | +// limitations under the License. | 
|  | 14 | + | 
|  | 15 | +package mcp | 
|  | 16 | + | 
|  | 17 | +import ( | 
|  | 18 | +	"fmt" | 
|  | 19 | +	"os" | 
|  | 20 | + | 
|  | 21 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/catalog" | 
|  | 22 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/config" | 
|  | 23 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/copy" | 
|  | 24 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/digest" | 
|  | 25 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/list" | 
|  | 26 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/manifest" | 
|  | 27 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/pull" | 
|  | 28 | +	"github.com/google/go-containerregistry/pkg/crane/mcp/tools/push" | 
|  | 29 | +	"github.com/mark3labs/mcp-go/server" | 
|  | 30 | +) | 
|  | 31 | + | 
|  | 32 | +// Config contains configuration options for the MCP server. | 
|  | 33 | +type Config struct { | 
|  | 34 | +	// Name is the server name. | 
|  | 35 | +	Name string | 
|  | 36 | +	// Version is the server version. | 
|  | 37 | +	Version string | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +// DefaultConfig returns the default configuration for the server. | 
|  | 41 | +func DefaultConfig() Config { | 
|  | 42 | +	return Config{ | 
|  | 43 | +		Name:    "crane-mcp", | 
|  | 44 | +		Version: "1.0.0", | 
|  | 45 | +	} | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +// Server is the crane MCP server. | 
|  | 49 | +type Server struct { | 
|  | 50 | +	mcpServer *server.MCPServer | 
|  | 51 | +	config    Config | 
|  | 52 | +} | 
|  | 53 | + | 
|  | 54 | +// New creates a new crane MCP server. | 
|  | 55 | +func New(cfg Config) *Server { | 
|  | 56 | +	s := &Server{ | 
|  | 57 | +		mcpServer: server.NewMCPServer(cfg.Name, cfg.Version), | 
|  | 58 | +		config:    cfg, | 
|  | 59 | +	} | 
|  | 60 | + | 
|  | 61 | +	// Register all tools | 
|  | 62 | +	s.registerTools() | 
|  | 63 | + | 
|  | 64 | +	return s | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +// registerTools registers all crane tools with the MCP server. | 
|  | 68 | +func (s *Server) registerTools() { | 
|  | 69 | +	// Register digest tool | 
|  | 70 | +	s.mcpServer.AddTool(digest.NewTool(), digest.Handle) | 
|  | 71 | + | 
|  | 72 | +	// Register pull tool | 
|  | 73 | +	s.mcpServer.AddTool(pull.NewTool(), pull.Handle) | 
|  | 74 | + | 
|  | 75 | +	// Register push tool | 
|  | 76 | +	s.mcpServer.AddTool(push.NewTool(), push.Handle) | 
|  | 77 | + | 
|  | 78 | +	// Register copy tool | 
|  | 79 | +	s.mcpServer.AddTool(copy.NewTool(), copy.Handle) | 
|  | 80 | + | 
|  | 81 | +	// Register catalog tool | 
|  | 82 | +	s.mcpServer.AddTool(catalog.NewTool(), catalog.Handle) | 
|  | 83 | + | 
|  | 84 | +	// Register list tool | 
|  | 85 | +	s.mcpServer.AddTool(list.NewTool(), list.Handle) | 
|  | 86 | + | 
|  | 87 | +	// Register config tool | 
|  | 88 | +	s.mcpServer.AddTool(config.NewTool(), config.Handle) | 
|  | 89 | + | 
|  | 90 | +	// Register manifest tool | 
|  | 91 | +	s.mcpServer.AddTool(manifest.NewTool(), manifest.Handle) | 
|  | 92 | +} | 
|  | 93 | + | 
|  | 94 | +// Serve starts the server with stdio. | 
|  | 95 | +func (s *Server) Serve() error { | 
|  | 96 | +	return server.ServeStdio(s.mcpServer) | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +// Run starts the server and exits the program if an error occurs. | 
|  | 100 | +func (s *Server) Run() { | 
|  | 101 | +	if err := s.Serve(); err != nil { | 
|  | 102 | +		fmt.Fprintf(os.Stderr, "Error: %v\n", err) | 
|  | 103 | +		os.Exit(1) | 
|  | 104 | +	} | 
|  | 105 | +} | 
0 commit comments