|
| 1 | +# Investigation: TypeScript `DiscoveryHttpServer` vs the Python SDK (#77) |
| 2 | + |
| 3 | +Checked against: Python `nitrostack-python-sdk` `develop` at `fb0faf8`; |
| 4 | +TypeScript `nitrocloudofficial/nitrostack` at `10859bef`. Date: 2026-11-03. |
| 5 | + |
| 6 | +## 1. What the TypeScript server does |
| 7 | +File: `typescript/packages/core/src/core/transports/discovery-http-server.ts` |
| 8 | + |
| 9 | +- Options (`:11-18`): `port`, `autoRetry`, `maxRetries`. |
| 10 | +- Defaults (`:53-67`): with an options object, `port` 3005, `autoRetry` true, |
| 11 | + `maxRetries` 10. With the legacy numeric constructor, `autoRetry` false. |
| 12 | +- Retry (`:23-45`, `:97-109`): when `autoRetry` is on, `findAvailablePort` probes |
| 13 | + `port`, `port+1`, ... up to `maxRetries` using a throwaway `net` server, then |
| 14 | + listens on the first free port. It throws if none is free. |
| 15 | +- Without `autoRetry`, an `EADDRINUSE` error is logged with a hint to set |
| 16 | + `OAUTH_DISCOVERY_PORT` and the start promise rejects (`:117-127`). |
| 17 | +- Reporting (`:93-95`): `getPort()` returns the port actually used. |
| 18 | +- Routing (`:72-87`): exact-match path handlers registered with `on(path, handler)`; |
| 19 | + unknown paths return 404. Bind address is not passed to `listen`, so Node's default applies. |
| 20 | + |
| 21 | +## 2. Consumers in the TypeScript stack |
| 22 | +| Consumer | File:line | Use | |
| 23 | +|---|---|---| |
| 24 | +| `OAuthModule.start()` | `core/oauth-module.ts:560-569` | In stdio mode, creates the server with `autoRetry` (env `OAUTH_DISCOVERY_AUTO_RETRY`, default on, `:549`) and `maxRetries: 50` (`:554`), reads `getPort()`, then emits the port to the client. | |
| 25 | +| `OAuthModule.start()` fallback | `core/oauth-module.ts:610-617` | Starts the same server if the HTTP transport is missing in http/dual mode. | |
| 26 | +| `registerDiscoveryHandlers` | `core/oauth-module.ts:667-673` | Registers `/.well-known/oauth-authorization-server`, `/.well-known/oauth-protected-resource`, and `/oauth/v2/register` when client registration is enabled. | |
| 27 | +| `notifyClientAboutOAuth` | `core/oauth-module.ts:639-657` | Writes `[NITROSTACK_OAUTH]{...}[/NITROSTACK_OAUTH]` to stderr with the actual port and well-known URLs. | |
| 28 | +| CLI template docs | `packages/cli/templates/typescript-oauth/OAUTH_SETUP.md:97,210,350` | Documents port 3005 and the startup log line. | |
| 29 | +| Tests | `transport.http.test.ts:111-172`, `transports.test.ts:244-288`, `oauth.extended.test.ts:39-41` | Cover the retry and no-retry options (ports 34508/34509). | |
| 30 | + |
| 31 | +Within `typescript/packages`, outside tests and templates, the only consumer is |
| 32 | +`OAuthModule`. No other code in that tree (including the CLI sources) references it. |
| 33 | +**Not checked:** NitroStudio's source is not in this repository, so whether Studio |
| 34 | +parses the `[NITROSTACK_OAUTH]` marker could not be verified. |
| 35 | + |
| 36 | +## 3. Current Python behaviour |
| 37 | +| Concern | Location | Behaviour | |
| 38 | +|---|---|---| |
| 39 | +| Startup | `nitrostack/core/app.py:1958-1966` | `McpApplication.start()` calls `OAuthService.start_discovery_server()` only when an `OAuthService` is registered in the container. | |
| 40 | +| Bind | `nitrostack/auth/oauth.py:259-274` | A daemon thread builds `HTTPServer(("localhost", port), DiscoveryHandler)` once. No probing and no retry. | |
| 41 | +| Port setting | `nitrostack/auth/oauth.py:261` | `int(os.environ.get("OAUTH_DISCOVERY_PORT", self.discovery_port))`: the **environment variable wins** over the `for_root(discovery_port=...)` value. `oauth_module.py` contains no port logic. | |
| 42 | +| Failure | `nitrostack/auth/oauth.py:275-277` | Any exception is written to stderr and swallowed. The thread ends, `_server` stays `None`, and the app keeps running with no discovery server. | |
| 43 | +| Reporting | `nitrostack/auth/oauth.py:264-273` | Writes `[NITROSTACK_OAUTH]{port, resource_uri, authorization_servers}[/NITROSTACK_OAUTH]` to stderr. The TypeScript marker carries more fields: `type`, `scopesSupported` and `wellKnownEndpoints`. | |
| 44 | +| CLI ports | `nitrostack/cli/main.py` `_resolve_runtime_ports`, `_normalize_port` | Resolve the MCP and widget ports (`--port`, `--widget`, env, defaults 3000/3001). Unrelated to OAuth discovery; no retry. | |
| 45 | + |
| 46 | +### Probe: preferred port already in use |
| 47 | +`scripts/probe_discovery_port.py` reserves a port, then starts the Python discovery server on it. |
| 48 | +Output on Windows (Python 3.12): |
| 49 | +busy port: 58380 |
| 50 | +Failed to start OAuth Discovery Server on port 58380: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions |
| 51 | +server object: None |
| 52 | +bound to: None |
| 53 | +GET failed: TimeoutError timed out |
| 54 | + |
| 55 | +The server does not start, no port is reported, and the application is not told. |
| 56 | +Note: Windows returned `WinError 10013` rather than the usual address-in-use error, |
| 57 | +which is how it reports binding to a port held by another socket here. |
| 58 | + |
| 59 | +## 4. Comparison |
| 60 | +| Capability | TypeScript | Python | Gap? | |
| 61 | +|---|---|---|---| |
| 62 | +| Separate discovery server | yes, stdio mode and as fallback | yes, always a background thread when OAuth is registered | no | |
| 63 | +| Preferred port | option, default 3005, env `OAUTH_DISCOVERY_PORT` | env or `discovery_port` | no | |
| 64 | +| Retry on busy port | `autoRetry`, `maxRetries` (50 in OAuthModule) | none | **yes** | |
| 65 | +| Actual port available to code | `getPort()` and `OAuthModule.discoveryInfo` | no | yes | |
| 66 | +| Port reported to the host tool | stderr marker with port and well-known URLs | stderr marker with port only | partial | |
| 67 | +| Behaviour on bind failure | start promise rejects | logged and swallowed; no server | yes | |
| 68 | +| Only started when OAuth is configured | yes | yes | no | |
| 69 | + |
| 70 | +## 5. Conclusion |
| 71 | +**Open an implementation issue; do not close this one.** The two servers match in |
| 72 | +purpose, endpoints and stderr marker. They differ in port conflicts: TypeScript tries |
| 73 | +up to 50 ports and reports the one it used, while Python makes one attempt and silently |
| 74 | +runs without discovery when the port is taken, as the probe shows. A client that reads |
| 75 | +the marker will then never receive a port. |
| 76 | + |
| 77 | +Proposed scope for the follow-up (link here once opened): |
| 78 | +- Retry on a busy port, controlled by `OAUTH_DISCOVERY_AUTO_RETRY` and a max-retries |
| 79 | + limit, with the same defaults as TypeScript (on, 50). |
| 80 | +- Store the actual port on `OAuthService` and put it in the stderr marker. |
| 81 | +- Surface bind failures instead of swallowing them. |
| 82 | +- Tests: busy port with retry, retries exhausted, port reported. |
| 83 | + |
| 84 | +Out of scope: matching the TypeScript marker's extra fields, until it is known |
| 85 | +whether NitroStudio consumes them. |
0 commit comments