From 38de88cd461100651bbb58a0c4e61d9eb2fde90a Mon Sep 17 00:00:00 2001 From: Derek Etherton Date: Mon, 30 Jun 2025 12:51:46 -0700 Subject: [PATCH] Allow the MCP server to start with no API token (requests will still fail), but not an invalid one --- .changes/unreleased/Changed-20250630-125123.yaml | 3 +++ src/cmd/client.go | 11 ++++++++--- src/cmd/root.go | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .changes/unreleased/Changed-20250630-125123.yaml diff --git a/.changes/unreleased/Changed-20250630-125123.yaml b/.changes/unreleased/Changed-20250630-125123.yaml new file mode 100644 index 0000000..d95069b --- /dev/null +++ b/.changes/unreleased/Changed-20250630-125123.yaml @@ -0,0 +1,3 @@ +kind: Changed +body: Allow the MCP Server to start with no OPSLEVEL_API_TOKEN so it can be inspected and tested. Tools will not work without a valid OpsLevel API token. +time: 2025-06-30T12:51:23.374588-07:00 diff --git a/src/cmd/client.go b/src/cmd/client.go index a19b3a2..0e560b9 100644 --- a/src/cmd/client.go +++ b/src/cmd/client.go @@ -11,17 +11,22 @@ import ( func NewGraphClient(version string, options ...opslevel.Option) *opslevel.Client { timeout := time.Second * time.Duration(viper.GetInt("api-timeout")) + api_token := viper.GetString("api-token") options = append( options, - opslevel.SetAPIToken(viper.GetString("api-token")), + opslevel.SetAPIToken(api_token), opslevel.SetURL(viper.GetString("api-url")), opslevel.SetTimeout(timeout), opslevel.SetUserAgentExtra(fmt.Sprintf("mcp-%s", version)), ) client := opslevel.NewGQLClient(options...) - clientErr := client.Validate() - cobra.CheckErr(clientErr) + // If API token is provided, ensure it's valid in OpsLevel to notify the user. + // If no token is provided, just allow the server to start for inspection. + if api_token != "" { + clientErr := client.Validate() + cobra.CheckErr(clientErr) + } return client } diff --git a/src/cmd/root.go b/src/cmd/root.go index 862239a..b2154e8 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -126,8 +126,9 @@ var rootCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { token := viper.GetString("api-token") + // Allow server to start even if token is missing if token == "" { - return fmt.Errorf("no API token was found, use --api-token=XXX or the OPSLEVEL_API_TOKEN environment variable is required") + log.Warn().Msg("No API token was found. Tool requests will fail with 401 Unauthorized. Set an API token using --api-token=XXX or the OPSLEVEL_API_TOKEN environment variable.") } s := server.NewMCPServer(