Move invariant api key check to __main__.py so that the same check can work for both mcp and server commands.

This commit is contained in:
Hemang
2025-05-16 09:59:43 +02:00
committed by Hemang Sarkar
parent f33b2392f6
commit 4dbb400620
3 changed files with 20 additions and 7 deletions
+12 -3
View File
@@ -10,6 +10,7 @@ import time
from typing import Optional
from gateway.mcp import mcp
from gateway.mcp.log import mcp_log
LOCAL_COMPOSE_FILE = "gateway/docker-compose.local.yml"
@@ -224,8 +225,18 @@ def main():
sys.exit(1)
verb = sys.argv[1]
if verb == "help":
print_help()
return 0
if "INVARIANT_API_KEY" not in os.environ:
print("[ERROR] INVARIANT_API_KEY environment variable is not set.")
mcp_log("[ERROR] INVARIANT_API_KEY environment variable is not set.")
sys.exit(1)
if verb == "mcp":
return asyncio.run(mcp.execute(sys.argv[2:]))
if verb == "server":
if len(sys.argv) < 3:
print(
@@ -239,9 +250,7 @@ def main():
if not run_server_command(command, args):
return 1
return 0
if verb == "help":
print_help()
return 0
print(f"[gateway/__main__.py] Unknown action: {verb}")
return 1
+8
View File
@@ -67,6 +67,14 @@ Key Parameters
* --push-explorer: Enables pushing annotated traces of MCP message flows to the specified project in Invariant Explorer.
### Logs
To see logs and errors from the MCP gateway and the MCP server, you can do:
```bash
tail -f ~/.invariant/mcp.log
```
### Local Development Workflow
To test or develop the gateway locally:
-4
View File
@@ -464,10 +464,6 @@ def split_args(args: list[str] = None) -> tuple[list[str], list[str]]:
async def execute(args: list[str] = None):
"""Main function to execute the MCP gateway."""
if "INVARIANT_API_KEY" not in os.environ:
mcp_log("[ERROR] INVARIANT_API_KEY environment variable is not set.")
sys.exit(1)
mcp_gateway_args, mcp_server_command_args = split_args(args)
ctx = McpContext(mcp_gateway_args)