Almaatla commited on
Commit
3ba2799
·
verified ·
1 Parent(s): 0ed457d

Upload 2 files

Browse files

correct access error

Files changed (2) hide show
  1. README.md +9 -0
  2. mcp_indexing.py +29 -0
README.md CHANGED
@@ -40,3 +40,12 @@ than called freely.
40
  ```
41
  claude mcp add --transport http docindexer-indexing https://<your-space>.hf.space/index/mcp
42
  ```
 
 
 
 
 
 
 
 
 
 
40
  ```
41
  claude mcp add --transport http docindexer-indexing https://<your-space>.hf.space/index/mcp
42
  ```
43
+
44
+ ### `MCP_ALLOWED_HOSTS` (only needed if you rename/fork the Space)
45
+
46
+ FastMCP enables DNS-rebinding protection and validates the incoming `Host` header. The
47
+ Space's current hostname (`organizedprogrammers-docindexer-v2.hf.space`) is allow-listed by
48
+ default in `mcp_indexing.py`. If you rename the Space or deploy a copy under a different
49
+ name, requests will get `421 Misdirected Request` until you either update that default or
50
+ set a Space variable `MCP_ALLOWED_HOSTS` to a comma-separated list of the extra hostnames to
51
+ allow, e.g. `my-other-space.hf.space`.
mcp_indexing.py CHANGED
@@ -9,6 +9,7 @@ SSE-formatted strings ("event: ...\\ndata: ...\\n\\n"). `_stream_generator` runs
9
  worker thread and forwards progress/info events to the MCP client via the tool Context,
10
  so the asyncio event loop is never blocked by the indexing work.
11
  """
 
12
  import queue
13
  import re
14
  import threading
@@ -18,6 +19,7 @@ import requests
18
  from bs4 import BeautifulSoup
19
  from mcp.server.fastmcp import Context, FastMCP
20
  from mcp.server.fastmcp.exceptions import ToolError
 
21
 
22
  from indexers import (
23
  get_folder_name,
@@ -28,6 +30,32 @@ from indexers import (
28
  meetings_mapping,
29
  )
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  indexing_mcp = FastMCP(
32
  name="docindexer-indexing",
33
  instructions=(
@@ -37,6 +65,7 @@ indexing_mcp = FastMCP(
37
  "(tdoc / 3gpp / etsi) can run at a time. Use list_meetings first to discover valid "
38
  "meeting folder names before calling index_tdocs_meeting."
39
  ),
 
40
  )
41
 
42
  _SSE_EVENT_RE = re.compile(r"event:\s*(\S+)\ndata:\s*(.*)", re.DOTALL)
 
9
  worker thread and forwards progress/info events to the MCP client via the tool Context,
10
  so the asyncio event loop is never blocked by the indexing work.
11
  """
12
+ import os
13
  import queue
14
  import re
15
  import threading
 
19
  from bs4 import BeautifulSoup
20
  from mcp.server.fastmcp import Context, FastMCP
21
  from mcp.server.fastmcp.exceptions import ToolError
22
+ from mcp.server.transport_security import TransportSecuritySettings
23
 
24
  from indexers import (
25
  get_folder_name,
 
30
  meetings_mapping,
31
  )
32
 
33
+
34
+ def _build_transport_security() -> TransportSecuritySettings:
35
+ """Allow the Host/Origin headers HF Spaces actually sends.
36
+
37
+ FastMCP defaults to `host="127.0.0.1"`, which auto-enables DNS-rebinding protection
38
+ scoped to localhost only (see mcp.server.fastmcp.server.FastMCP.__init__). Behind the HF
39
+ Spaces proxy the Host header is the public `*.hf.space` hostname, so that default rejects
40
+ every request with 421. Set MCP_ALLOWED_HOSTS (comma-separated, e.g.
41
+ "organizedprogrammers-docindexer-v2.hf.space") as a Space variable to extend this list if
42
+ the Space is renamed or duplicated.
43
+ """
44
+ hosts = {"127.0.0.1:*", "localhost:*", "[::1]:*", "organizedprogrammers-docindexer-v2.hf.space"}
45
+ origins = {"http://127.0.0.1:*", "http://localhost:*", "http://[::1]:*"}
46
+ for host in os.environ.get("MCP_ALLOWED_HOSTS", "").split(","):
47
+ host = host.strip()
48
+ if host:
49
+ hosts.add(host)
50
+ for host in hosts - {"127.0.0.1:*", "localhost:*", "[::1]:*"}:
51
+ origins.add(f"https://{host}")
52
+ return TransportSecuritySettings(
53
+ enable_dns_rebinding_protection=True,
54
+ allowed_hosts=sorted(hosts),
55
+ allowed_origins=sorted(origins),
56
+ )
57
+
58
+
59
  indexing_mcp = FastMCP(
60
  name="docindexer-indexing",
61
  instructions=(
 
65
  "(tdoc / 3gpp / etsi) can run at a time. Use list_meetings first to discover valid "
66
  "meeting folder names before calling index_tdocs_meeting."
67
  ),
68
+ transport_security=_build_transport_security(),
69
  )
70
 
71
  _SSE_EVENT_RE = re.compile(r"event:\s*(\S+)\ndata:\s*(.*)", re.DOTALL)