|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package mcp |
| 19 | + |
| 20 | +import ( |
| 21 | + stdlog "log" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/mark3labs/mcp-go/server" |
| 25 | + "github.com/sirupsen/logrus" |
| 26 | + "github.com/streamnative/streamnative-mcp-server/pkg/mcp" |
| 27 | +) |
| 28 | + |
| 29 | +func newMcpServer(configOpts *ServerOptions, logrusLogger *logrus.Logger) *server.MCPServer { |
| 30 | + snConfig := configOpts.Options.LoadConfigOrDie() |
| 31 | + var s *server.MCPServer |
| 32 | + switch { |
| 33 | + case snConfig.KeyFile != "": |
| 34 | + { |
| 35 | + issuer := snConfig.Auth.Issuer() |
| 36 | + userName, err := configOpts.Options.WhoAmI(issuer.Audience) |
| 37 | + if err != nil { |
| 38 | + stdlog.Fatalf("failed to get user name: %v", err) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + // Create a new MCP server |
| 42 | + s = server.NewMCPServer( |
| 43 | + "streamnative-mcp-server", |
| 44 | + "0.0.1", |
| 45 | + server.WithResourceCapabilities(true, true), |
| 46 | + server.WithInstructions(mcp.GetStreamNativeCloudServerInstructions(userName, snConfig)), |
| 47 | + server.WithLogging()) |
| 48 | + |
| 49 | + mcp.RegisterPrompts(s) |
| 50 | + mcp.RegisterContextTools(s, configOpts.Features) |
| 51 | + mcp.StreamNativeAddLogTools(s, configOpts.ReadOnly, configOpts.Features) |
| 52 | + mcp.StreamNativeAddResourceTools(s, configOpts.ReadOnly, configOpts.Features) |
| 53 | + } |
| 54 | + case snConfig.ExternalKafka != nil: |
| 55 | + { |
| 56 | + s = server.NewMCPServer( |
| 57 | + "streamnative-mcp-server/kafka", |
| 58 | + "0.0.1", |
| 59 | + server.WithResourceCapabilities(true, true), |
| 60 | + server.WithInstructions(mcp.GetExternalKafkaServerInstructions(snConfig.ExternalKafka.BootstrapServers)), |
| 61 | + server.WithLogging()) |
| 62 | + } |
| 63 | + case snConfig.ExternalPulsar != nil: |
| 64 | + { |
| 65 | + s = server.NewMCPServer( |
| 66 | + "streamnative-mcp-server/pulsar", |
| 67 | + "0.0.1", |
| 68 | + server.WithResourceCapabilities(true, true), |
| 69 | + server.WithInstructions(mcp.GetExternalPulsarServerInstructions(snConfig.ExternalPulsar.WebServiceURL)), |
| 70 | + server.WithLogging()) |
| 71 | + } |
| 72 | + default: |
| 73 | + { |
| 74 | + stdlog.Fatalf("no valid configuration found") |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + mcp.PulsarAdminAddBrokersTools(s, configOpts.ReadOnly, configOpts.Features) |
| 80 | + mcp.PulsarAdminAddBrokerStatsTools(s, configOpts.ReadOnly, configOpts.Features) |
| 81 | + mcp.PulsarAdminAddClusterTools(s, configOpts.ReadOnly, configOpts.Features) |
| 82 | + mcp.PulsarAdminAddFunctionsWorkerTools(s, configOpts.ReadOnly, configOpts.Features) |
| 83 | + mcp.PulsarAdminAddNamespaceTools(s, configOpts.ReadOnly, configOpts.Features) |
| 84 | + mcp.PulsarAdminAddNamespacePolicyTools(s, configOpts.ReadOnly, configOpts.Features) |
| 85 | + mcp.PulsarAdminAddNsIsolationPolicyTools(s, configOpts.ReadOnly, configOpts.Features) |
| 86 | + mcp.PulsarAdminAddPackagesTools(s, configOpts.ReadOnly, configOpts.Features) |
| 87 | + mcp.PulsarAdminAddResourceQuotasTools(s, configOpts.ReadOnly, configOpts.Features) |
| 88 | + mcp.PulsarAdminAddSchemasTools(s, configOpts.ReadOnly, configOpts.Features) |
| 89 | + mcp.PulsarAdminAddSubscriptionTools(s, configOpts.ReadOnly, configOpts.Features) |
| 90 | + mcp.PulsarAdminAddTenantTools(s, configOpts.ReadOnly, configOpts.Features) |
| 91 | + mcp.PulsarAdminAddTopicTools(s, configOpts.ReadOnly, configOpts.Features) |
| 92 | + mcp.PulsarAdminAddSinksTools(s, configOpts.ReadOnly, configOpts.Features) |
| 93 | + mcp.PulsarAdminAddFunctionsTools(s, configOpts.ReadOnly, configOpts.Features) |
| 94 | + mcp.PulsarAdminAddSourcesTools(s, configOpts.ReadOnly, configOpts.Features) |
| 95 | + mcp.PulsarAdminAddTopicPolicyTools(s, configOpts.ReadOnly, configOpts.Features) |
| 96 | + mcp.PulsarClientAddConsumerTools(s, configOpts.ReadOnly, configOpts.Features) |
| 97 | + mcp.PulsarClientAddProducerTools(s, configOpts.ReadOnly, configOpts.Features) |
| 98 | + |
| 99 | + mcp.KafkaAdminAddTopicTools(s, configOpts.ReadOnly, configOpts.Features) |
| 100 | + mcp.KafkaAdminAddPartitionsTools(s, configOpts.ReadOnly, configOpts.Features) |
| 101 | + mcp.KafkaAdminAddGroupsTools(s, configOpts.ReadOnly, configOpts.Features) |
| 102 | + mcp.KafkaAdminAddSchemaRegistryTools(s, configOpts.ReadOnly, configOpts.Features) |
| 103 | + mcp.KafkaAdminAddKafkaConnectTools(s, configOpts.ReadOnly, configOpts.Features) |
| 104 | + mcp.KafkaClientAddConsumeTools(s, configOpts.ReadOnly, logrusLogger, configOpts.Features) |
| 105 | + mcp.KafkaClientAddProduceTools(s, configOpts.ReadOnly, configOpts.Features) |
| 106 | + return s |
| 107 | +} |
0 commit comments