Skip to content

Commit 24ae82f

Browse files
The MCP server's tools answer from the catalogue of operations (#2122)
Part of #1684 Each tool called the library's methods itself. Its answers now come from the entries of Core/Operations/Catalogue.cs: Catalogue.Solve.Ask(e, x) where it called e.Solve(x), and so on. Every entry answers exactly what its method answers, so no answer changes, and the 335 tests that drive the server pass as they did. The server's own checks of an answer stay direct calls: differentiating an antiderivative back, reading an input as an integer or a decimal, and the guard before quadrature. Tools.Offers says which entries each tool offers. Tools.OwnComputations lists the six tools whose answer is the server's own computation, such as am_check_steps, each with what it computes. Tools.NotOffered lists the two entries no tool offers, normalize and evaluate, each with the reason. The server's CatalogueTest checks the three against the tool list and the catalogue. An entry added to the catalogue fails it until a tool offers it or NotOffered says why none does. amcli's one-shot commands get the same check, without calling the catalogue: OneShot.offers names the entry each command offers, ownCommands says what info, mcp and help do, and notOffered lists the entries no command offers. Which commands to add is #1019 item 16's question. OneShotTest reads the catalogue's names by reflection, since the terminal is not one of the library's friends. Removing one tool and one entry from the server's lists fails both of its tests and names each, and the same for one command and one entry in the terminal's. The catalogue is internal, so the library names the server in its InternalsVisibleTo. A strong-named assembly may only befriend another one, so the server is signed with the library's key, as the unit tests are, and so is McpUnitTests, which the server befriends. The server ships inside amcli with the library it was built against. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 5999db4 commit 24ae82f

9 files changed

Lines changed: 259 additions & 58 deletions

File tree

‎AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,9 @@ landed — and **the MCP server and two other repositories brought to the releas
878878
MCP still says what the library does. An operation the library offers by name is an entry of
879879
`Core/Operations/Catalogue.cs` ([#1684](https://github.com/asc-community/AngouriMath/issues/1684)),
880880
and a new factory of `Transformation` fails `CatalogueTest` until it is an entry or a building
881-
block with its reason.
881+
block with its reason. The MCP server's tools answer from those entries, and a new entry fails
882+
the server's own `CatalogueTest` until a tool offers it or `Tools.NotOffered` says why none does,
883+
and the terminal's `OneShotTest` until a command offers it or `OneShot.notOffered` lists it.
882884
- [CSharpMath](https://github.com/verybadcat/CSharpMath), for the round trip of `Latexize` — see
883885
*The standard command wins* above. If a PR there from an earlier release is still unmerged, add
884886
to it rather than opening a second.

‎Sources/AngouriMath/Functions/InternalAMExtensions.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
+ "552aeb08b7a67f49a5557ef4faab3d9f033182be1a646b24c932399732107462848dcb97acecc844"
3838
+ "9997b1b77e6bd337e1116878226dd4954e004f11193ccd8e29fc156615c798f733712923ffe8d6c5")]
3939

40+
// And to the MCP server behind `amcli mcp`, whose tools answer from the catalogue of operations in
41+
// Core/Operations, which stays internal until v3. The server is no package of its own: it ships
42+
// inside the terminal's tool with the library it was built against, so it never meets another
43+
// version's internals. https://github.com/asc-community/AngouriMath/issues/1684
44+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("AngouriMath.Mcp, PublicKey="
45+
+ "0024000004800000940000000602000000240000525341310004000001000100f54ef0f5905ba32e"
46+
+ "cae04751103c5565283a4c4fa2b07627ff7a7e556b287f198203a09935d998ce9d812a696aa2e71c"
47+
+ "552aeb08b7a67f49a5557ef4faab3d9f033182be1a646b24c932399732107462848dcb97acecc844"
48+
+ "9997b1b77e6bd337e1116878226dd4954e004f11193ccd8e29fc156615c798f733712923ffe8d6c5")]
49+
4050
namespace AngouriMath
4151
{
4252
// Visibility for the class is internal so we can use public for methods as we like

‎Sources/MCP/AngouriMath.Mcp/AngouriMath.Mcp.csproj‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@
1818
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1919
<!-- Shipped inside the terminal's tool package, not as a package of its own. -->
2020
<IsPackable>false</IsPackable>
21+
<!-- Signed with the library's key, so that the library's InternalsVisibleTo can name it and
22+
the tools can answer from its internal catalogue of operations: a strong-named assembly
23+
may only befriend another strong-named one. It ships with the library it was built
24+
against, so it never meets another version's internals.
25+
https://github.com/asc-community/AngouriMath/issues/1684 -->
26+
<SignAssembly>true</SignAssembly>
27+
<AssemblyOriginatorKeyFile>../../AngouriMath/key.snk</AssemblyOriginatorKeyFile>
2128
</PropertyGroup>
2229

2330
<ItemGroup>
24-
<InternalsVisibleTo Include="McpUnitTests" />
31+
<!-- Its tests are signed with the same key, which a signed assembly's friends have to be. -->
32+
<InternalsVisibleTo Include="McpUnitTests" Key="0024000004800000940000000602000000240000525341310004000001000100f54ef0f5905ba32ecae04751103c5565283a4c4fa2b07627ff7a7e556b287f198203a09935d998ce9d812a696aa2e71c552aeb08b7a67f49a5557ef4faab3d9f033182be1a646b24c932399732107462848dcb97acecc8449997b1b77e6bd337e1116878226dd4954e004f11193ccd8e29fc156615c798f733712923ffe8d6c5" />
2533
</ItemGroup>
2634

2735
<ItemGroup>

‎Sources/MCP/AngouriMath.Mcp/Tools.cs‎

Lines changed: 118 additions & 56 deletions
Large diffs are not rendered by default.

‎Sources/MCP/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ Each of these looks arbitrary and is not:
159159
corrupts the stream, and the client reports the server as failed.
160160
- **Every library call goes through `Guard.Run`.** It is what keeps a stack overflow or a hang
161161
inside one request.
162+
- **A tool's answer comes from the library's catalogue of operations**, `Core/Operations/Catalogue.cs`,
163+
and `Tools.Offers` says which entries each tool offers. An entry added there fails
164+
`CatalogueTest` until a tool offers it or `Tools.NotOffered` says why none does, so the server
165+
keeps up with the library by construction. The server's own checks of an answer, such as
166+
differentiating an antiderivative back, call the library directly. The catalogue is internal,
167+
which is why this assembly is signed with the library's key.
162168
- **Decline detection reads the raw result, before any `Simplify`.** An unevaluated `limit(...)`
163169
simplifies to `NaN`, which would turn an honest decline into what looks like a wrong answer.
164170
- **Simplify before stripping a `provided` guard, never after.** The guard is what licenses the

‎Sources/Terminal/AngouriMath.Terminal.Lib/OneShot.fs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ An argument written as _ is read from stdin, and so is one that is left out:
3737
/// The names <c>run</c> answers. Anything else as a first argument is a usage error.
3838
let commands = set [ "eval"; "simp"; "fsimp"; "diff"; "solve"; "sub"; "latex"; "info"; "mcp"; "help"; "-h"; "--help" ]
3939

40+
/// <summary>
41+
/// The operations of the library's catalogue, <c>Core/Operations/Catalogue.cs</c>, that each command
42+
/// offers, by the entries' names. A test checks this against <see cref="commands"/> and the
43+
/// catalogue: every command offers an entry or is in <see cref="ownCommands"/>, and every entry is
44+
/// offered or is in <see cref="notOffered"/>.
45+
/// https://github.com/asc-community/AngouriMath/issues/1684
46+
/// </summary>
47+
let offers =
48+
Map [ "eval", [ "evaluate" ]
49+
"simp", [ "simplify" ]
50+
"fsimp", [ "normalize" ]
51+
"diff", [ "differentiate" ]
52+
"solve", [ "solve" ]
53+
"sub", [ "substitute" ]
54+
"latex", [ "latex" ] ]
55+
56+
/// The commands that are not one of the catalogue's operations, each with what it does.
57+
let ownCommands =
58+
Map [ "info", "prints the variables, derivatives, roots and stationary points, which several operations answer together"
59+
"mcp", "serves the library to an agent"
60+
"help", "prints the usage"
61+
"-h", "prints the usage"
62+
"--help", "prints the usage" ]
63+
64+
/// <summary>
65+
/// The catalogue's entries that no command offers. The one-shot commands are the eight of
66+
/// asc-community/AngouriMathCLI, kept so that its scripts keep working, and which others to add is
67+
/// the question of #1019 item 16; an entry added to the catalogue joins this list or a command.
68+
/// </summary>
69+
let notOffered =
70+
set [ "expand"; "factorize"; "integrate"; "limit"; "solve_system"; "evaluate_numerically"; "variables"
71+
"alternatives"; "sympy"; "truth_table"; "satisfying_assignments"; "series"; "to_base"; "from_base"
72+
"determinant"; "inverse"; "transpose"; "rank"; "reduced_row_echelon_form"; "trace"; "tensor_product"
73+
"prime_factorization"; "totient"; "greatest_common_divisor"; "divisor_count"; "is_prime" ]
74+
4075
/// The arguments still to read, falling back to a line of stdin where one is missing or is <c>_</c>.
4176
type private Arguments(given: string list, input: TextReader) =
4277
let mutable rest = given
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using Xunit;
9+
10+
namespace AngouriMath.Mcp.Tests;
11+
12+
/// <summary>
13+
/// The tools against the library's catalogue of operations: every tool offers an entry or says
14+
/// what it computes itself, and every entry is offered by a tool or is listed with the reason
15+
/// none offers it. An operation added to the catalogue fails here until the server takes it.
16+
/// https://github.com/asc-community/AngouriMath/issues/1684
17+
/// </summary>
18+
public sealed class CatalogueTest
19+
{
20+
private static readonly HashSet<string> ToolNames =
21+
Tools.List(preliminary: true).Select(tool => tool!["name"]!.GetValue<string>()).ToHashSet();
22+
23+
[Fact]
24+
public void EveryToolOffersAnEntryOrSaysWhatItComputes()
25+
{
26+
var unaccounted = ToolNames.Where(tool => !Tools.OfferedNames.ContainsKey(tool) && !Tools.OwnComputations.ContainsKey(tool));
27+
Assert.Empty(unaccounted);
28+
Assert.Empty(Tools.OfferedNames.Keys.Intersect(Tools.OwnComputations.Keys));
29+
Assert.Empty(Tools.OfferedNames.Keys.Concat(Tools.OwnComputations.Keys).Where(tool => !ToolNames.Contains(tool)));
30+
Assert.All(Tools.OfferedNames, tool => Assert.NotEmpty(tool.Value));
31+
}
32+
33+
[Fact]
34+
public void EveryEntryIsOfferedOrSaysWhyNot()
35+
{
36+
var offered = Tools.OfferedNames.Values.SelectMany(names => names).ToHashSet();
37+
var catalogue = Tools.CatalogueNames.ToHashSet();
38+
Assert.Empty(catalogue.Where(entry => !offered.Contains(entry) && !Tools.NotOffered.ContainsKey(entry)));
39+
Assert.Empty(offered.Intersect(Tools.NotOffered.Keys));
40+
Assert.Empty(Tools.NotOffered.Keys.Where(entry => !catalogue.Contains(entry)));
41+
}
42+
}

‎Sources/Tests/McpUnitTests/McpUnitTests.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<ImplicitUsings>enable</ImplicitUsings>
1313
<IsPackable>false</IsPackable>
1414
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
15+
<!-- Signed with the library's key, as the server it tests is, so that the server's
16+
InternalsVisibleTo can name it. -->
17+
<SignAssembly>true</SignAssembly>
18+
<AssemblyOriginatorKeyFile>../../AngouriMath/key.snk</AssemblyOriginatorKeyFile>
1519
</PropertyGroup>
1620

1721
<ItemGroup>

‎Sources/Tests/TerminalUnitTests/OneShotTest.fs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,35 @@ let ``info names the variables and gives the derivative and the roots for each``
127127
Assert.Contains("variables: x", lines)
128128
Assert.Contains("derivative with respect to x: 2 * x", lines)
129129
Assert.Contains(lines, fun line -> line.StartsWith("roots for x: ") && line.Contains("-2") && line.Contains("2"))
130+
131+
/// The names of the library's catalogue of operations, which is internal, so read here by reflection.
132+
/// https://github.com/asc-community/AngouriMath/issues/1684
133+
let private catalogue =
134+
let read (name: string) (instance: obj | null) (owner: System.Type | null) =
135+
match owner with
136+
| null -> failwith "the library has no catalogue"
137+
| owner ->
138+
match owner.GetProperty name with
139+
| null -> failwith $"{owner.Name} has no {name}"
140+
| property -> property.GetValue instance
141+
match read "All" null (typeof<AngouriMath.Entity>.Assembly.GetType "AngouriMath.Core.Operations.Catalogue") with
142+
| :? System.Collections.IEnumerable as all ->
143+
set [ for operation in all do
144+
match operation with
145+
| null -> ()
146+
| operation -> yield string (read "Name" operation (operation.GetType())) ]
147+
| _ -> failwith "Catalogue.All is not a list"
148+
149+
[<Fact>]
150+
let ``every command offers an entry of the catalogue or says what it does`` () =
151+
for command in OneShot.commands do
152+
Assert.True(OneShot.offers.ContainsKey command <> OneShot.ownCommands.ContainsKey command, command)
153+
for command in Seq.append (OneShot.offers |> Map.keys) (OneShot.ownCommands |> Map.keys) do
154+
Assert.Contains(command, OneShot.commands)
155+
156+
[<Fact>]
157+
let ``every entry of the catalogue is offered by a command or listed as not offered`` () =
158+
let offered = OneShot.offers |> Map.values |> Seq.concat |> set
159+
Assert.Empty(catalogue - offered - OneShot.notOffered)
160+
Assert.Empty(Set.intersect offered OneShot.notOffered)
161+
Assert.Empty(offered + OneShot.notOffered - catalogue)

0 commit comments

Comments
 (0)