Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(mcp): preserve parent for local file uploads (#4521)

LeoFree65f60beb4898
diff --git a/openviking/server/mcp_endpoint.py b/openviking/server/mcp_endpoint.py
index 095bafe50..3feb968ec 100644
--- a/openviking/server/mcp_endpoint.py
+++ b/openviking/server/mcp_endpoint.py
@@ -972,8 +972,8 @@ async def add_resource(
to: Target URI under viking://resources/ (e.g. "viking://resources/volcengine/OpenViking").
Required when ``add_type`` is set; otherwise leave empty to derive a URI
from the source.
- parent: Parent URI under viking://resources/ for remote imports. Mutually exclusive
- with ``to`` and not supported when ``add_type`` is set.
+ parent: Parent URI under viking://resources/ for remote or local-file imports.
+ Mutually exclusive with ``to`` and not supported when ``add_type`` is set.
tags: Optional explicit k=v retrieval tags to apply after ingestion.
tag_mode: Tag update mode, "replace" or "append". Defaults to "replace".
args: Parser-specific options, e.g. {"auth_config": {"token": "..."}}
@@ -1021,6 +1021,8 @@ async def add_resource(
return "Error: add_type cannot be combined with parent."
if add_type and not to:
return "Error: add_type requires an exact 'to' target."
+ if to and parent:
+ return "Error: Cannot specify both 'to' and 'parent' at the same time."
# Branch 1: ingest by temp_file_id. Kept for backward compat / REST-style use — the
# signed upload now auto-ingests server-side, so agents no longer need this second leg.
@@ -1035,6 +1037,7 @@ async def add_resource(
temp_file_id,
ctx,
to=to,
+ parent=parent,
reason=description,
args=args,
processing_mode=processing_mode,
@@ -1133,6 +1136,7 @@ async def add_resource(
ctx.user.user_id,
ttl_seconds=ttl_seconds,
to=to,
+ parent=parent,
reason=description,
actor_peer_id=ctx.actor_peer_id or "",
processing_mode=processing_mode,
diff --git a/openviking/server/resource_ingest.py b/openviking/server/resource_ingest.py
index 9f3aacf3e..992baba29 100644
--- a/openviking/server/resource_ingest.py
+++ b/openviking/server/resource_ingest.py
@@ -25,6 +25,7 @@ async def ingest_temp_upload(
ctx: RequestContext,
*,
to: str = "",
+ parent: str = "",
reason: str = "",
args: Optional[dict[str, Any]] = None,
processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,
@@ -55,6 +56,7 @@ async def ingest_temp_upload(
path=resolved.local_path,
ctx=ctx,
to=to or None,
+ parent=parent or None,
reason=reason,
source_name=resolved.original_filename,
wait=False,
diff --git a/openviking/server/routers/resources.py b/openviking/server/routers/resources.py
index 8828c2938..429e6e44f 100644
--- a/openviking/server/routers/resources.py
+++ b/openviking/server/routers/resources.py
@@ -190,6 +190,7 @@ async def temp_upload(
temp_file_id,
_ctx,
to=signed.to,
+ parent=signed.parent,
reason=signed.reason,
processing_mode=signed.processing_mode,
tags=signed.tags,
diff --git a/openviking/server/upload_token_store.py b/openviking/server/upload_token_store.py
index 046698be8..811b3a814 100644
--- a/openviking/server/upload_token_store.py
+++ b/openviking/server/upload_token_store.py
@@ -9,7 +9,7 @@ key, no on-disk state, no replay-set bookkeeping. ``dict.pop`` doubles as the
consume-and-burn primitive.
The token carries the identity bound at issue time (account/user), the caller's actor peer
-scope (``actor_peer_id``), and the business params (``to``/``reason``/``parse_mode``) so the server can
+scope (``actor_peer_id``), and the business params (``to``/``parent``/``reason``/``parse_mode``) so the server can
finish ingestion automatically once the file lands — the caller does not re-invoke
``add_resource``, and the ingest keeps the original peer scope. The ``temp_file_id`` is minted by
:class:`openviking.server.temp_upload_store.TempUploadStore` at upload time, so the token
@@ -44,6 +44,7 @@ class _TokenInfo:
account_id: str
user_id: str
to: str
+ parent: str
reason: str
actor_peer_id: str
processing_mode: ProcessingMode
@@ -62,6 +63,7 @@ class ConsumedUploadToken:
to: str
reason: str
actor_peer_id: str
+ parent: str = ""
processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE
tags: Optional[list[str]] = None
tag_mode: str = "replace"
@@ -79,6 +81,7 @@ class UploadTokenStore:
ttl_seconds: int,
*,
to: str = "",
+ parent: str = "",
reason: str = "",
actor_peer_id: str = "",
processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,
@@ -86,7 +89,7 @@ class UploadTokenStore:
tag_mode: str = "replace",
parse_mode: str = "default",
) -> Tuple[str, float]:
- """Mint a fresh token bound to (account, user) plus ``to``/``reason``/``actor_peer_id``.
+ """Mint a fresh token bound to the caller identity and resource ingestion parameters.
``actor_peer_id`` is captured from the minting request's context so server-side
auto-ingest keeps the caller's peer scope (reason-memory routing) — it is NOT taken
@@ -99,6 +102,7 @@ class UploadTokenStore:
account_id,
user_id,
to,
+ parent,
reason,
actor_peer_id,
processing_mode,
@@ -124,15 +128,16 @@ class UploadTokenStore:
if info.expires_at < time.time():
raise UploadTokenError("upload token expired")
return ConsumedUploadToken(
- info.account_id,
- info.user_id,
- info.to,
- info.reason,
- info.actor_peer_id,
- info.processing_mode,
- info.tags,
- info.tag_mode,
- info.parse_mode,
+ account_id=info.account_id,
+ user_id=info.user_id,
+ to=info.to,
+ parent=info.parent,
+ reason=info.reason,
+ actor_peer_id=info.actor_peer_id,
+ processing_mode=info.processing_mode,
+ tags=info.tags,
+ tag_mode=info.tag_mode,
+ parse_mode=info.parse_mode,
)
def peek(self, token: str) -> Optional[_TokenInfo]:
diff --git a/tests/server/test_mcp_parse_mode.py b/tests/server/test_mcp_parse_mode.py
index 609e222a0..97b14de5d 100644
--- a/tests/server/test_mcp_parse_mode.py
+++ b/tests/server/test_mcp_parse_mode.py
@@ -105,6 +105,39 @@ async def test_mcp_local_upload_token_binds_no_split(
assert token_info.parse_mode == "no_split"
+async def test_mcp_local_upload_token_binds_parent(
+ monkeypatch: pytest.MonkeyPatch,
+ mcp_context,
+):
+ monkeypatch.setattr(
+ mcp_endpoint,
+ "_resolve_public_base_url",
+ lambda: ("https://ov.example.com", "config"),
+ )
+
+ await mcp_endpoint.add_resource(
+ path="/tmp/manual.pdf",
+ parent="viking://~/resources/team",
+ )
+
+ assert len(upload_token_store._store) == 1
+ token_info = next(iter(upload_token_store._store.values()))
+ assert token_info.parent == "viking://user/test_user/resources/team"
+
+
+async def test_mcp_rejects_to_and_parent_together(mcp_context):
+ result = await mcp_endpoint.add_resource(
+ path="/tmp/manual.pdf",
+ to="viking://resources/exact-target",
+ parent="viking://~/resources/team",
+ )
+
+ assert result == "Error: Cannot specify both 'to' and 'parent' at the same time."
+ assert upload_token_store._store == {}
+
+
@pytest.mark.asyncio
async def test_mcp_rejects_invalid_parse_mode(mcp_context):
result = await mcp_endpoint.add_resource(
diff --git a/tests/server/test_resource_ingest_parse_mode.py b/tests/server/test_resource_ingest_parse_mode.py
index b152f24fe..62e07e908 100644
--- a/tests/server/test_resource_ingest_parse_mode.py
+++ b/tests/server/test_resource_ingest_parse_mode.py
@@ -40,8 +40,10 @@ async def test_ingest_temp_upload_forwards_no_split(monkeypatch: pytest.MonkeyPa
store,
"upload-id",
ctx,
+ parent="viking://user/test_user/resources/team",
parse_mode="no_split",
)
assert add_resource.await_args.kwargs["args"] == {"parse_mode": "no_split"}
assert "parse_mode" not in add_resource.await_args.kwargs
+ assert add_resource.await_args.kwargs["parent"] == "viking://user/test_user/resources/team"
diff --git a/tests/server/test_resource_parse_mode_request.py b/tests/server/test_resource_parse_mode_request.py
index 9a283737a..2dddab6ef 100644
--- a/tests/server/test_resource_parse_mode_request.py
+++ b/tests/server/test_resource_parse_mode_request.py
@@ -7,6 +7,7 @@ from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
+
from openviking.server.identity import RequestContext, Role
from openviking.server.routers import resources as resources_router
from openviking.server.routers.resources import AddResourceRequest
@@ -38,7 +39,7 @@ async def test_add_resource_route_forwards_no_split_mode(monkeypatch: pytest.Mon
)
await resources_router.add_resource(
- SimpleNamespace(),
+ SimpleNamespace(headers={}),
AddResourceRequest(
path="https://example.com/demo.md",
args={"parse_mode": "no_split"},
@@ -57,6 +58,7 @@ async def test_signed_temp_upload_forwards_token_bound_parse_mode(
account_id="test_account",
user_id="test_user",
to="",
+ parent="viking://user/test_user/resources/team",
reason="",
actor_peer_id="",
parse_mode="no_split",
@@ -87,3 +89,4 @@ async def test_signed_temp_upload_forwards_token_bound_parse_mode(
)
assert ingest.await_args.kwargs["parse_mode"] == "no_split"
+ assert ingest.await_args.kwargs["parent"] == "viking://user/test_user/resources/team"
diff --git a/tests/server/test_resources_temp_upload_token.py b/tests/server/test_resources_temp_upload_token.py
index 64277d26b..96fb3e8fd 100644
--- a/tests/server/test_resources_temp_upload_token.py
+++ b/tests/server/test_resources_temp_upload_token.py
@@ -30,6 +30,7 @@ def _issue(
user_id: str = "user",
*,
to: str = "",
+ parent: str = "",
reason: str = "",
actor_peer_id: str = "",
tags: list[str] | None = None,
@@ -40,6 +41,7 @@ def _issue(
user_id,
ttl_seconds=600,
to=to,
+ parent=parent,
reason=reason,
actor_peer_id=actor_peer_id,
tags=tags,
@@ -57,6 +59,7 @@ def _stub_ingest(service, monkeypatch, root_uri: str = "viking://resources/uploa
captured["content"] = Path(path).read_bytes()
captured["ctx"] = ctx
captured["to"] = kwargs.get("to")
+ captured["parent"] = kwargs.get("parent")
captured["reason"] = kwargs.get("reason")
captured["tags"] = kwargs.get("tags")
captured["tag_mode"] = kwargs.get("tag_mode")
@@ -102,6 +105,21 @@ async def test_token_upload_forwards_to_and_reason(
assert captured["reason"] == "quarterly report"
+async def test_token_upload_forwards_parent(
+ client: httpx.AsyncClient, service, upload_temp_dir: Path, monkeypatch
+):
+ captured = _stub_ingest(service, monkeypatch)
+ token = _issue(parent="viking://user/user/resources/team")
+ resp = await client.post(
+ "/api/v1/resources/temp_upload",
+ params={"token": token},
+ files={"file": ("r.md", b"data", "text/markdown")},
+ )
+
+ assert resp.status_code == 200, resp.text
+ assert captured["parent"] == "viking://user/user/resources/team"
+
+
async def test_token_upload_forwards_tags_and_tag_mode(
client: httpx.AsyncClient, service, upload_temp_dir: Path, monkeypatch
):
diff --git a/tests/server/test_upload_token_store.py b/tests/server/test_upload_token_store.py
index 13baad07e..e28a2375c 100644
--- a/tests/server/test_upload_token_store.py
+++ b/tests/server/test_upload_token_store.py
@@ -31,7 +31,12 @@ def test_consume_roundtrip(store):
consumed = store.consume(token)
assert (consumed.account_id, consumed.user_id) == ("acct", "user")
# Business params default to empty when not supplied at issue time.
- assert (consumed.to, consumed.reason, consumed.actor_peer_id) == ("", "", "")
+ assert (consumed.to, consumed.parent, consumed.reason, consumed.actor_peer_id) == (
+ "",
+ "",
+ "",
+ "",
+ )
assert consumed.parse_mode == "default"
@@ -41,6 +46,7 @@ def test_consume_returns_bound_business_params(store):
"user",
ttl_seconds=60,
to="viking://resources/team/proj",
+ parent="viking://user/user/resources/team",
reason="quarterly",
actor_peer_id="bot-a",
processing_mode="vectors_only",
@@ -48,6 +54,7 @@ def test_consume_returns_bound_business_params(store):
)
consumed = store.consume(token)
assert consumed.to == "viking://resources/team/proj"
+ assert consumed.parent == "viking://user/user/resources/team"
assert consumed.reason == "quarterly"
assert consumed.actor_peer_id == "bot-a"
assert consumed.processing_mode == "vectors_only"