Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
137 lines (118 loc) · 4.34 KB
/
Copy pathserver.py
File metadata and controls
137 lines (118 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""``everos server`` subcommand group.
Provides ``everos server start`` to run the HTTP API via uvicorn. CLI
parses arguments, configures structured logging, then hands off to
uvicorn pointing at :func:`everos.entrypoints.api.app.create_app` as a
factory.
"""
from __future__ import annotations
import os
import sys
import typer
import uvicorn
from everos.config import load_settings
from everos.config.settings import resolve_root
from everos.core.observability.logging import get_logger
from everos.core.utils.open_files import low_limit_warning, raise_open_file_limit
from everos.service import configure_process
app = typer.Typer(
name="server",
help="Run / manage the HTTP API server",
no_args_is_help=True,
)
# The wheel ships only ``src/everos``, so the guide has to be linked, not pathed.
MCP_GUIDE_URL = "https://github.com/EverMind-AI/EverOS/blob/main/docs/mcp.md"
_WILDCARD_BINDS = {"0.0.0.0", "::", ""}
def mcp_base_url(host: str, port: int) -> str:
"""The ``EVEROS_BASE_URL`` a client on this machine should use.
``host`` is the bind address: a wildcard bind (``0.0.0.0`` / ``::``) is
not something a client can connect to, so it becomes loopback; IPv6
literals are bracketed so the result is a valid URL.
"""
if host in _WILDCARD_BINDS:
host = "127.0.0.1"
elif ":" in host and not host.startswith("["):
host = f"[{host}]"
return f"http://{host}:{port}"
@app.command("start")
def start(
host: str | None = typer.Option(
None,
"--host",
help="Bind host (env: EVEROS_API__HOST, default: 127.0.0.1)",
),
port: int | None = typer.Option(
None,
"--port",
help="Bind port (env: EVEROS_API__PORT, default: 8000)",
),
root: str | None = typer.Option(
None,
"--root",
help="Memory root directory (env: EVEROS_ROOT, default: ~/.everos)",
),
reload: bool = typer.Option(
False,
"--reload",
help="Reload on source changes (development)",
),
log_level: str | None = typer.Option(
None,
"--log-level",
help="Log level (env: EVEROS_LOG_LEVEL; default: logging.level, INFO)",
),
) -> None:
"""Start the HTTP API server."""
if root:
os.environ["EVEROS_ROOT"] = root
resolved_root = resolve_root(root)
everos_toml = resolved_root / "everos.toml"
if not everos_toml.is_file():
typer.secho(
f"Error: {everos_toml} not found.\n"
f"Run `everos init` first to create configuration files.",
fg=typer.colors.RED,
err=True,
)
raise typer.Exit(code=1)
level_override = log_level or os.getenv("EVEROS_LOG_LEVEL")
if level_override:
# The app factory loads its own settings (in a fresh process under
# --reload), so the override travels the way --root does: as env.
os.environ["EVEROS_LOGGING__LEVEL"] = level_override.upper()
settings = load_settings()
configure_process(settings)
host_resolved = host or settings.api.host
port_resolved = port if port is not None else settings.api.port
bootstrap_logger = get_logger("everos.cli.server")
bootstrap_logger.info("starting everos on %s:%d", host_resolved, port_resolved)
limits = raise_open_file_limit()
if limits is not None:
bootstrap_logger.info("open-file soft limit %d -> %d", *limits)
if warning := low_limit_warning(limits):
bootstrap_logger.warning(warning)
bootstrap_logger.info(
"once up, MCP clients connect with: uvx everos-mcp, "
"EVEROS_BASE_URL=%s (guide: %s)",
mcp_base_url(host_resolved, port_resolved),
MCP_GUIDE_URL,
)
if host_resolved == "0.0.0.0":
bootstrap_logger.warning(
"binding to 0.0.0.0 exposes the API on all interfaces; EverOS "
"ships no built-in auth — see SECURITY.md"
)
try:
uvicorn.run(
"everos.entrypoints.api.app:create_app",
host=host_resolved,
port=port_resolved,
reload=reload,
factory=True,
log_level=settings.logging.level.lower(),
log_config=None,
)
except KeyboardInterrupt:
bootstrap_logger.info("interrupted; shutting down")
except (OSError, RuntimeError) as exc:
bootstrap_logger.error("startup failed: %s", exc)
sys.exit(1)