backend: let a model put a figure in the paper, and set a formula's alignment
Two things the editor gained and MCP could not do, both because the tool layer
had been told about the domain's columns by hand.
**A figure.** Bytes go to MinIO and the sentence keeps a Markdown address, so a
picture is ordinary text — which means the writing tools already carry it, and
the only half a model cannot do is the upload: it has bytes and no way to send
multipart. `image_upload` takes base64 and answers with the URL, the Markdown
and the *sentence object* the editor produces when a picture is dropped in, so a
figure is written by putting that object into `sentences` unchanged. It does not
fetch a URL the caller names: a service inside the cluster that will GET any
address it is handed is an SSRF primitive aimed at everything beside it.
The width argument is the reader's rule or it is refused. `{width=70%}` is
parsed by `frontend/src/utils/rich.ts`, and a value it cannot read is not an
error there — the picture just prints at its natural size. A size that silently
does not apply is worse than a refusal, so the syntax and the range (pixels
1–4000, per cent 1–100) are mirrored in `app/mcp/tools/images.py`, with the
usual comment naming the other side.
`paragraph_text` also had to learn what a figure is: a sentence holding nothing
but an image is a 插图 — the view prints it on its own line with the brackets'
text as its caption — and the text a tool hands back broke lines around a
display formula but not around a picture. Same rule as `figureOf`, same pair of
implementations, same warning about changing both.
**A formula's alignment.** `paper_sentence.formula_align` reached the editor and
the REST API and stopped there: `SentenceSpec` had no such field, so a model that
sent one got a successful call and a centred formula. The column is now declared,
carried through every write path and reported by every read path.
That is the second column lost the same way, so the fix is structural rather
than another two edits. `SENTENCE_FIELDS` is read from
`app.schemas.paper.SentenceInput` instead of written out, the payload is built
by one `sentence_payload()`, and the three append paths spread
`SentenceCreate(**item.model_dump())` rather than copying fields. One edit
remains — declaring the field in `specs.py` — and smoke_mcp.py's first step
compares the two field sets, so forgetting it now fails the suite instead of
losing the field. `sort` is declared too, which it never was: a model that sent
one had it dropped in silence.
smoke_mcp.py: 99 checks to 119. The figure step uploads a real 1x1 GIF (the same
bytes twice, and the key is the same object), writes the returned sentence as it
came back, checks the paragraph prints it alone on its line and that an image
inside a sentence does not break the line, and takes the four refusals — not a
picture, an unparseable size, an out-of-range one, not base64. The alignment step
writes a right-aligned formula, moves the sentence to left on its own, and takes
the paragraph to another position to prove the setting travels with it.
Deployed as v6, and the pod now carries MINIO_ENDPOINT (the in-cluster Service)
plus the two credential keys from the Secret; without them the image tool answers
with the missing variable names and every writing tool still works.
This commit is contained in:
@@ -23,8 +23,10 @@ Exits non-zero on the first failed expectation and cleans up after itself.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from collections.abc import AsyncIterator, Iterator
|
||||
from contextlib import asynccontextmanager, contextmanager
|
||||
@@ -43,10 +45,21 @@ from mcp.client.stdio import StdioServerParameters, stdio_client # noqa: E402
|
||||
from sqlalchemy import or_, select # noqa: E402
|
||||
|
||||
from app.db.session import SessionLocal # noqa: E402
|
||||
from app.mcp.specs import SentenceSpec # noqa: E402
|
||||
from app.models import Paper, Reference, ReferenceTag, Template # noqa: E402
|
||||
from app.schemas.paper import SentenceInput # noqa: E402
|
||||
|
||||
SERVER_SCRIPT = BACKEND_DIR / "scripts" / "mcp_server.py"
|
||||
|
||||
#: A 1x1 GIF, and something that is definitely not a picture. Both are inline
|
||||
#: rather than files on disk: the suite has to run from a checkout with nothing
|
||||
#: beside it, and a fixture that cannot go missing cannot fail for the wrong
|
||||
#: reason.
|
||||
GIF_1PX_BASE64 = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
|
||||
NOT_A_PICTURE_BASE64 = base64.b64encode(
|
||||
b'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>'
|
||||
).decode()
|
||||
|
||||
_checks = 0
|
||||
_created_papers: list[int] = []
|
||||
_created_templates: list[int] = []
|
||||
@@ -169,7 +182,7 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
print("\n1. tool discovery")
|
||||
listed = await session.list_tools()
|
||||
names = {tool.name for tool in listed.tools}
|
||||
check("the whole surface is published", len(names) == 41, f"got {len(names)}")
|
||||
check("the whole surface is published", len(names) == 42, f"got {len(names)}")
|
||||
check(
|
||||
"every group is reachable",
|
||||
{
|
||||
@@ -180,10 +193,33 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
"field_list",
|
||||
"reference_create",
|
||||
"reference_tag_create",
|
||||
"image_upload",
|
||||
}
|
||||
<= names,
|
||||
", ".join(sorted(names)),
|
||||
)
|
||||
# The prefix is how a model finds a tool in a list of forty: a tool whose
|
||||
# name does not start with its group's prefix is a tool nothing reaches
|
||||
# for, and the mistake is invisible in every other check.
|
||||
check(
|
||||
"every tool name starts with its group's prefix",
|
||||
all(
|
||||
name.startswith(
|
||||
(
|
||||
"paper_",
|
||||
"paragraph_",
|
||||
"sentence_",
|
||||
"template_",
|
||||
"field_",
|
||||
"reference_",
|
||||
"reference_tag_",
|
||||
"image_",
|
||||
)
|
||||
)
|
||||
for name in names
|
||||
),
|
||||
", ".join(sorted(names)),
|
||||
)
|
||||
check(
|
||||
"no tool returns a structured envelope",
|
||||
all(tool.output_schema is None for tool in listed.tools),
|
||||
@@ -193,6 +229,24 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
"paper_write describes its paragraph array",
|
||||
"paragraphs" in write_tool.input_schema["properties"],
|
||||
)
|
||||
# A sentence column that this surface does not declare is a column no
|
||||
# model can set — which is exactly how `line_break_before` and then
|
||||
# `formula_align` were lost: the write succeeded and the field was
|
||||
# silently gone. Declaring every field is one line; forgetting one fails
|
||||
# here.
|
||||
missing = set(SentenceInput.model_fields) - set(SentenceSpec.model_fields)
|
||||
check(
|
||||
"the sentence schema declares every writable field of a sentence",
|
||||
not missing,
|
||||
f"missing from SentenceSpec: {sorted(missing)}",
|
||||
)
|
||||
add_tool = next(tool for tool in listed.tools if tool.name == "sentence_add")
|
||||
align = add_tool.input_schema["properties"]["formula_align"]
|
||||
check(
|
||||
"and a formula's alignment is an enum, so a model cannot invent one",
|
||||
align.get("enum") == ["center", "left", "right"],
|
||||
json.dumps(align, ensure_ascii=False),
|
||||
)
|
||||
|
||||
print("\n2. create a paper on that template")
|
||||
paper = await call(
|
||||
@@ -864,7 +918,213 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
repr(as_text["text"][-300:]),
|
||||
)
|
||||
|
||||
print("\n18. move a paragraph, then clear the one it moved onto")
|
||||
print("\n18. a figure goes in, and prints on a line of its own")
|
||||
# The bytes go to the object store and the sentence keeps the address,
|
||||
# so this is the one place the suite needs MinIO as well as the database.
|
||||
# The 1x1 GIF is the smallest real picture there is, which is all that is
|
||||
# needed to prove the bytes arrive and the address comes back.
|
||||
figure = await call(
|
||||
session,
|
||||
"image_upload",
|
||||
data_base64=f"data:image/gif;base64,{GIF_1PX_BASE64}",
|
||||
caption="图 1 试验装置示意图",
|
||||
width="70%",
|
||||
)
|
||||
check(
|
||||
"the key is the shape the store writes, named after the bytes",
|
||||
re.fullmatch(r"\d{4}/\d{2}/[0-9a-f]{16}\.gif", figure["key"]) is not None,
|
||||
figure["key"],
|
||||
)
|
||||
check(
|
||||
"the URL is same-origin under the API prefix",
|
||||
figure["url"] == f"/api/images/{figure['key']}",
|
||||
figure["url"],
|
||||
)
|
||||
check(
|
||||
"the markdown is the sentence's content, caption and size included",
|
||||
figure["markdown"]
|
||||
== f"{{width=70%}}",
|
||||
figure["markdown"],
|
||||
)
|
||||
check(
|
||||
"and the sentence that shows it is ready to write as it stands",
|
||||
figure["sentence"]
|
||||
== {
|
||||
"content": figure["markdown"],
|
||||
"line_break_before": True,
|
||||
"formula_align": "center",
|
||||
},
|
||||
json.dumps(figure["sentence"], ensure_ascii=False),
|
||||
)
|
||||
|
||||
same = await call(
|
||||
session, "image_upload", data_base64=GIF_1PX_BASE64, filename="另一张.png"
|
||||
)
|
||||
check(
|
||||
"the same picture again is the same object, not a second copy",
|
||||
same["key"] == figure["key"],
|
||||
f"{same['key']} vs {figure['key']}",
|
||||
)
|
||||
check(
|
||||
"and the caption defaults to the filename's stem",
|
||||
same["markdown"] == f"",
|
||||
same["markdown"],
|
||||
)
|
||||
|
||||
# The returned sentence object is written as it came back, unchanged:
|
||||
# that is the whole contract of the tool.
|
||||
await call(
|
||||
session,
|
||||
"sentence_add",
|
||||
paper_id=paper["id"],
|
||||
heading=outline[3]["name"],
|
||||
**figure["sentence"],
|
||||
)
|
||||
with_figure = await call(
|
||||
session, "paragraph_get", paper_id=paper["id"], heading=outline[3]["name"]
|
||||
)
|
||||
check(
|
||||
"the figure is stored as the sentence it was handed",
|
||||
with_figure["sentences"][-1]["content"] == figure["markdown"]
|
||||
and with_figure["sentences"][-1]["line_break_before"] is True,
|
||||
json.dumps(with_figure["sentences"][-1], ensure_ascii=False),
|
||||
)
|
||||
check(
|
||||
"and the paragraph prints it alone on its own line",
|
||||
with_figure["text"].endswith("\n" + figure["markdown"]),
|
||||
repr(with_figure["text"][-200:]),
|
||||
)
|
||||
|
||||
inline_url = same["url"]
|
||||
inline = f"试验装置如  所示。"
|
||||
await call(
|
||||
session,
|
||||
"sentence_add",
|
||||
paper_id=paper["id"],
|
||||
heading=outline[3]["name"],
|
||||
content=inline,
|
||||
)
|
||||
with_inline = await call(
|
||||
session, "paragraph_get", paper_id=paper["id"], heading=outline[3]["name"]
|
||||
)
|
||||
check(
|
||||
"an image inside a sentence stays on the line with the words",
|
||||
f"\n{inline}" in with_inline["text"]
|
||||
and f"\n" not in with_inline["text"],
|
||||
repr(with_inline["text"][-260:]),
|
||||
)
|
||||
|
||||
message = await expect_error(
|
||||
session,
|
||||
"image_upload",
|
||||
data_base64=NOT_A_PICTURE_BASE64,
|
||||
filename="图.svg",
|
||||
)
|
||||
check(
|
||||
"a file that is not a picture is refused, naming the formats",
|
||||
"PNG" in message and "SVG" in message,
|
||||
message,
|
||||
)
|
||||
tolerant = await call(
|
||||
session,
|
||||
"image_upload",
|
||||
data_base64=GIF_1PX_BASE64,
|
||||
caption="写法宽容",
|
||||
width="{w = 400 px}",
|
||||
)
|
||||
check(
|
||||
"how the size is written is forgiving, what it means is not",
|
||||
tolerant["markdown"] == f"{{width=400px}}",
|
||||
tolerant["markdown"],
|
||||
)
|
||||
message = await expect_error(
|
||||
session, "image_upload", data_base64=GIF_1PX_BASE64, width="70em"
|
||||
)
|
||||
check(
|
||||
"a size the reader cannot parse is refused rather than quietly ignored",
|
||||
"70%" in message,
|
||||
message,
|
||||
)
|
||||
message = await expect_error(
|
||||
session, "image_upload", data_base64=GIF_1PX_BASE64, width="150%"
|
||||
)
|
||||
check(
|
||||
"and so is one that parses but the page would drop on the floor",
|
||||
"1 到 100" in message,
|
||||
message,
|
||||
)
|
||||
message = await expect_error(
|
||||
session, "image_upload", data_base64="这不是 base64"
|
||||
)
|
||||
check(
|
||||
"and a value that is not base64 says what to send instead",
|
||||
"base64" in message,
|
||||
message,
|
||||
)
|
||||
|
||||
print("\n19. a formula says how it is set on its line")
|
||||
derivation = outline[2]
|
||||
await call(
|
||||
session,
|
||||
"paper_write",
|
||||
paper_id=paper["id"],
|
||||
paragraphs=[
|
||||
{
|
||||
"heading": derivation["name"],
|
||||
"sentences": [
|
||||
"推导如下。",
|
||||
{
|
||||
"content": "$$ a^2 + b^2 = c^2 $$",
|
||||
"line_break_before": True,
|
||||
"formula_align": "right",
|
||||
},
|
||||
],
|
||||
}
|
||||
],
|
||||
)
|
||||
aligned = await call(
|
||||
session, "paragraph_get", paper_id=paper["id"], heading=derivation["name"]
|
||||
)
|
||||
check(
|
||||
"the alignment is stored on the sentence that carries the formula",
|
||||
[row["formula_align"] for row in aligned["sentences"]]
|
||||
== ["center", "right"],
|
||||
json.dumps(aligned["sentences"], ensure_ascii=False),
|
||||
)
|
||||
turned = await call(
|
||||
session,
|
||||
"sentence_update",
|
||||
paper_id=paper["id"],
|
||||
sentence_id=aligned["sentences"][-1]["id"],
|
||||
formula_align="left",
|
||||
)
|
||||
check(
|
||||
"and it can be changed without touching the formula",
|
||||
turned["sentence"]["formula_align"] == "left"
|
||||
and turned["sentence"]["content"] == "$$ a^2 + b^2 = c^2 $$",
|
||||
json.dumps(turned["sentence"], ensure_ascii=False),
|
||||
)
|
||||
await call(
|
||||
session,
|
||||
"paragraph_move",
|
||||
paper_id=paper["id"],
|
||||
heading=derivation["name"],
|
||||
target_heading=outline[3]["name"],
|
||||
)
|
||||
carried = await call(
|
||||
session, "paragraph_get", paper_id=paper["id"], heading=outline[3]["name"]
|
||||
)
|
||||
check(
|
||||
"and it travels with the sentence when the paragraph moves",
|
||||
any(
|
||||
row["content"] == "$$ a^2 + b^2 = c^2 $$"
|
||||
and row["formula_align"] == "left"
|
||||
for row in carried["sentences"]
|
||||
),
|
||||
json.dumps(carried["sentences"], ensure_ascii=False),
|
||||
)
|
||||
|
||||
print("\n20. move a paragraph, then clear the one it moved onto")
|
||||
home = outline[0]
|
||||
target = outline[1]
|
||||
relocation = await call(
|
||||
@@ -896,7 +1156,7 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
after_clear = await call(session, "paragraph_get", paper_id=paper["id"], position=target["position"])
|
||||
check("and the paragraph is empty", after_clear["sentences"] == [], json.dumps(after_clear, ensure_ascii=False))
|
||||
|
||||
print("\n19. the field library round-trips")
|
||||
print("\n21. the field library round-trips")
|
||||
field = await call(session, "field_create", name="SMOKE MCP 字段", level=2, font_size=9.5)
|
||||
check("created with its typography", field["level"] == 2 and field["font_size"] == 9.5, json.dumps(field, ensure_ascii=False))
|
||||
renamed = await call(session, "field_update", field_id=field["id"], name="SMOKE MCP 字段(改)")
|
||||
@@ -905,7 +1165,7 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
check("deleted while no template places it", dropped["deleted"] == 1, json.dumps(dropped, ensure_ascii=False))
|
||||
check("and it is reported as found, not missing", dropped["missing"] == [], json.dumps(dropped, ensure_ascii=False))
|
||||
|
||||
print("\n20. switch the template and keep the content")
|
||||
print("\n22. switch the template and keep the content")
|
||||
spare = await call(
|
||||
session,
|
||||
"template_create",
|
||||
@@ -949,15 +1209,15 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
json.dumps(switched_document, ensure_ascii=False)[:400],
|
||||
)
|
||||
|
||||
print("\n21. a template in use cannot be deleted")
|
||||
print("\n23. a template in use cannot be deleted")
|
||||
message = await expect_error(session, "template_delete", template_ids=spare["id"])
|
||||
check("the refusal names the blocker", "SMOKE MCP 模板" in message, message)
|
||||
|
||||
print("\n22. deleting a paper needs an explicit confirmation")
|
||||
print("\n24. deleting a paper needs an explicit confirmation")
|
||||
message = await expect_error(session, "paper_delete", paper_ids=paper["id"])
|
||||
check("the refusal explains itself", "confirm=true" in message, message)
|
||||
|
||||
print("\n23. delete the paper and the template")
|
||||
print("\n25. delete the paper and the template")
|
||||
deleted = await call(session, "paper_delete", paper_ids=paper["id"], confirm=True)
|
||||
check("deleted", deleted["deleted"] == 1, json.dumps(deleted, ensure_ascii=False))
|
||||
_created_papers.remove(paper["id"])
|
||||
|
||||
Reference in New Issue
Block a user