Commit 38a5480
Python: Default MCP SSE server samples to loopback with host validation (#14127)
## Motivation and Context
The Python MCP server demos under `python/samples/demos/mcp_server/` can
optionally run over the SSE transport (`--transport sse`). This updates
that sample wiring to follow the Model Context Protocol guidance for
local development servers, so developers who use these demos as a
starting point inherit sensible defaults.
## Description
- **Loopback by default**: the SSE samples now bind to `127.0.0.1`
instead of `0.0.0.0`. A new `--host` argument makes binding to other
interfaces an explicit opt-in that logs a warning.
- **Host/Origin validation**: added Starlette `TrustedHostMiddleware`
plus a small Origin allowlist middleware so the local listener only
serves loopback callers (requests without an `Origin` header are still
allowed, for non-browser MCP clients).
- **Sample hygiene**: switched `Starlette(debug=True)` to `debug=False`
so the demos don't ship verbose debug output.
- **Docs**: the README now describes the loopback-by-default behavior,
the `--host` opt-in, and points to the existing `mcp_with_oauth` sample
for authenticated, network-reachable deployments.
Applies to `sk_mcp_server.py` and `agent_as_server.py`. The stdio
transport (the default) and the other stdio-only samples are unchanged.
## Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] I didn't break anyone 😄
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent efa3268 commit 38a5480
3 files changed
Lines changed: 189 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
64 | 78 | | |
65 | 79 | | |
66 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
54 | 65 | | |
55 | 66 | | |
56 | 67 | | |
| |||
66 | 77 | | |
67 | 78 | | |
68 | 79 | | |
69 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
70 | 94 | | |
71 | 95 | | |
72 | 96 | | |
| |||
88 | 112 | | |
89 | 113 | | |
90 | 114 | | |
91 | | - | |
| 115 | + | |
92 | 116 | | |
93 | 117 | | |
94 | 118 | | |
| |||
110 | 134 | | |
111 | 135 | | |
112 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
113 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
114 | 184 | | |
115 | 185 | | |
116 | 186 | | |
| |||
122 | 192 | | |
123 | 193 | | |
124 | 194 | | |
125 | | - | |
| 195 | + | |
126 | 196 | | |
127 | 197 | | |
128 | 198 | | |
129 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
130 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
131 | 214 | | |
132 | | - | |
| 215 | + | |
133 | 216 | | |
134 | 217 | | |
135 | 218 | | |
| |||
142 | 225 | | |
143 | 226 | | |
144 | 227 | | |
145 | | - | |
| 228 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
57 | 68 | | |
58 | 69 | | |
59 | 70 | | |
| |||
69 | 80 | | |
70 | 81 | | |
71 | 82 | | |
72 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
73 | 97 | | |
74 | 98 | | |
75 | | - | |
| 99 | + | |
76 | 100 | | |
77 | 101 | | |
78 | 102 | | |
| |||
112 | 136 | | |
113 | 137 | | |
114 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
115 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
116 | 186 | | |
117 | 187 | | |
118 | 188 | | |
| |||
121 | 191 | | |
122 | 192 | | |
123 | 193 | | |
124 | | - | |
| 194 | + | |
125 | 195 | | |
126 | 196 | | |
127 | 197 | | |
128 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
129 | 203 | | |
130 | 204 | | |
131 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
132 | 214 | | |
133 | 215 | | |
134 | 216 | | |
| |||
142 | 224 | | |
143 | 225 | | |
144 | 226 | | |
145 | | - | |
| 227 | + | |
0 commit comments