| 1 |
|
| /* pmcp_decl.h - tool declarations, checked and frozen at to_app. |
| 2 |
|
| * |
| 3 |
|
| * Needs: pmcp_reg.h, pmcp_proto.h. |
| 4 |
|
| * Gives: pmcp_tools (the frozen table), pmcp_tools_freeze, pmcp_tools_free, |
| 5 |
|
| * pmcp_tool_find, pmcp_json_trapped. |
| 6 |
|
| * |
| 7 |
|
| * A tool's list entry is encoded once per protocol version at boot, because |
| 8 |
|
| * the fields differ by version: title and outputSchema arrived in 2025-06-18, |
| 9 |
|
| * icons in 2025-11-25. tools/list then only concatenates bytes. |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #ifndef PMCP_DECL_H |
| 13 |
|
| #define PMCP_DECL_H |
| 14 |
|
| |
| 15 |
|
| #define PMCP_TOOL_NAME_MAX 128 |
| 16 |
|
| |
| 17 |
|
| typedef struct { |
| 18 |
|
| SV *name; /* owned */ |
| 19 |
|
| SV *target; /* code ref, owned */ |
| 20 |
|
| SV *input; /* JSON::Schema::Fast::Compiled, owned */ |
| 21 |
|
| SV *output; /* the same, or NULL */ |
| 22 |
|
| AV *guards; /* resolved code refs, or NULL for none */ |
| 23 |
|
| NV timeout; /* seconds, 0 for none */ |
| 24 |
|
| int stream; |
| 25 |
|
| SV *entry[PMCP_V_LATEST + 1]; /* encoded tools/list entry, by version */ |
| 26 |
|
| /* an OpenAPI tool (12): no target, a request to re-enter the app with */ |
| 27 |
|
| SV *oa_method; /* GET, POST, ... */ |
| 28 |
|
| SV *oa_path; /* the mount prefix and the path template */ |
| 29 |
|
| HV *oa_where; /* argument -> {in, name} */ |
| 30 |
|
| } pmcp_tool; |
| 31 |
|
| |
| 32 |
|
| typedef struct { |
| 33 |
|
| pmcp_tool *t; /* sorted by name */ |
| 34 |
|
| IV n; |
| 35 |
|
| IV page_size; |
| 36 |
|
| int any_guard; /* the list is filtered per caller */ |
| 37 |
|
| SV **list[PMCP_V_LATEST + 1]; /* t[i].entry[v] in name order, borrowed */ |
| 38 |
|
| } pmcp_tools; |
| 39 |
|
| |
| 40 |
|
| /* ---- encoding a value someone else built ---------------------------------- */ |
| 41 |
|
| |
| 42 |
|
| static SV *PMCP_ENCODE = NULL; /* \&File::Raw::JSON::file_json_encode */ |
| 43 |
|
| |
| 44 |
|
| /* Canonical JSON of a value an application handed back. Through the XSUB |
| 45 |
|
| * under G_EVAL, because the value can hold anything (a code ref, a glob) and |
| 46 |
|
| * the encoder croaks on what it cannot encode. Returns +1, or NULL with *err |
| 47 |
|
| * set (+1). */ |
| 48 |
1663 |
| static SV *pmcp_json_trapped(pTHX_ SV *value, SV **err) |
| 49 |
|
| { |
| 50 |
|
| SV *argv[3]; |
| 51 |
|
| SV *out; |
| 52 |
1663 |
▶ | if (!PMCP_ENCODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PMCP_ENCODE) { |
36 |
1627 |
1663 |
|
| 53 |
36 |
| CV *cv = get_cv("File::Raw::JSON::file_json_encode", 0); |
| 54 |
36 |
▶ | if (!cv) croak(PMCP_DIST ": File::Raw::JSON has no file_json_encode"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cv) croak(PMCP_DIST ": File::Raw::JSON has no file_json_encode"); |
0 |
36 |
36 |
|
| 55 |
36 |
| PMCP_ENCODE = newRV_inc((SV *)cv); |
| 56 |
|
| } |
| 57 |
1663 |
| argv[0] = value; |
| 58 |
1663 |
| argv[1] = sv_2mortal(newSVpvs("canonical")); |
| 59 |
1663 |
| argv[2] = sv_2mortal(newSViv(1)); |
| 60 |
1663 |
| out = pmcp_call_sv(aTHX_ PMCP_ENCODE, argv, 3, G_SCALAR, err); |
| 61 |
1663 |
▶ | if (out && !SvOK(out)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && !SvOK(out)) { |
1662 |
1 |
1663 |
| if (out && !SvOK(out)) { |
0 |
1662 |
1662 |
|
| 62 |
0 |
| SvREFCNT_dec(out); |
| 63 |
0 |
▶ | if (err) *err = newSVpvs("the encoder returned nothing"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = newSVpvs("the encoder returned nothing"); |
0 |
0 |
0 |
|
| 64 |
0 |
| return NULL; |
| 65 |
|
| } |
| 66 |
1663 |
| return out; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* ---- checking a declaration ---------------------------------------------- */ |
| 70 |
|
| |
| 71 |
|
| static const char *const PMCP_TOOL_KEYS[] = { |
| 72 |
|
| "title", "description", "input", "output", "annotations", "stream", |
| 73 |
|
| "guard", "icons", "timeout", NULL |
| 74 |
|
| }; |
| 75 |
|
| |
| 76 |
|
| static const char *const PMCP_ANNOTATION_KEYS[] = { |
| 77 |
|
| "title", "readOnlyHint", "destructiveHint", "idempotentHint", |
| 78 |
|
| "openWorldHint", NULL |
| 79 |
|
| }; |
| 80 |
|
| |
| 81 |
3 |
| static void pmcp_tool_croak(pTHX_ SV *name, const char *what) |
| 82 |
|
| { |
| 83 |
3 |
| croak("%s: mcp tool '%" SVf "' %s", PMCP_WHO, SVfARG(name), what); |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
230 |
| static int pmcp_tool_name_ok(const char *p, STRLEN l) |
| 87 |
|
| { |
| 88 |
|
| STRLEN i; |
| 89 |
230 |
▶ | if (l < 1 || l > PMCP_TOOL_NAME_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l < 1 || l > PMCP_TOOL_NAME_MAX) return 0; |
230 |
0 |
230 |
| if (l < 1 || l > PMCP_TOOL_NAME_MAX) return 0; |
1 |
229 |
230 |
|
| 90 |
1465 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
1237 |
228 |
1465 |
|
| 91 |
1237 |
| char c = p[i]; |
| 92 |
1239 |
▶ | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
Branch: 5 branches
| Branch | True | False | Total |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
1227 |
10 |
1237 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
1227 |
0 |
1227 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
1183 |
54 |
1237 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
0 |
1183 |
1183 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
1 |
1 |
2 |
|
| 93 |
54 |
▶ | || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-')) |
Branch: 4 branches
| Branch | True | False | Total |
| || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-')) |
51 |
3 |
54 |
| || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-')) |
44 |
7 |
51 |
| || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-')) |
3 |
44 |
47 |
| || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-')) |
2 |
1 |
3 |
|
| 94 |
1 |
| return 0; |
| 95 |
|
| } |
| 96 |
228 |
| return 1; |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
|
| /* An object schema: a hash whose `type` is "object". */ |
| 100 |
23 |
| static void pmcp_check_object_schema(pTHX_ SV *name, SV *schema, const char *k) |
| 101 |
|
| { |
| 102 |
|
| SV *t; |
| 103 |
23 |
▶ | if (!pmcp_is_hash(schema)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_hash(schema)) { |
1 |
22 |
23 |
|
| 104 |
1 |
| croak("%s: mcp tool '%" SVf "' %s must be a JSON Schema hash", |
| 105 |
|
| PMCP_WHO, SVfARG(name), k); |
| 106 |
|
| } |
| 107 |
22 |
| t = pmcp_hget(aTHX_ (HV *)SvRV(schema), "type"); |
| 108 |
44 |
▶ | if (!(t && !SvROK(t) && strEQ(SvPV_nolen(t), "object"))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(t && !SvROK(t) && strEQ(SvPV_nolen(t), "object"))) |
22 |
0 |
22 |
| if (!(t && !SvROK(t) && strEQ(SvPV_nolen(t), "object"))) |
22 |
0 |
22 |
| if (!(t && !SvROK(t) && strEQ(SvPV_nolen(t), "object"))) |
2 |
20 |
22 |
|
| 109 |
2 |
| croak("%s: mcp tool '%" SVf "' %s must have type 'object'", PMCP_WHO, |
| 110 |
|
| SVfARG(name), k); |
| 111 |
20 |
| } |
| 112 |
|
| |
| 113 |
|
| /* Compile a schema with JSON::Schema::Fast's own constructor, under G_EVAL so |
| 114 |
|
| * a bad one croaks naming the tool. resolver => undef: a tool's schema is part |
| 115 |
|
| * of the application, and booting must not fetch a remote $ref. +1. */ |
| 116 |
344 |
| static SV *pmcp_compile_schema(pTHX_ SV *name, SV *schema, const char *k) |
| 117 |
|
| { |
| 118 |
344 |
| SV *argv[3], *err = NULL, *c; |
| 119 |
344 |
| argv[0] = schema; |
| 120 |
344 |
| argv[1] = sv_2mortal(newSVpvs("resolver")); |
| 121 |
344 |
| argv[2] = &PL_sv_undef; |
| 122 |
344 |
| c = pmcp_call_method(aTHX_ sv_2mortal(newSVpvs("JSON::Schema::Fast")), |
| 123 |
|
| "compile", argv, 3, G_SCALAR, &err); |
| 124 |
344 |
▶ | if (!c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) { |
1 |
343 |
344 |
|
| 125 |
1 |
| SV *why = sv_2mortal(err); |
| 126 |
1 |
| croak("%s: mcp tool '%" SVf "' %s does not compile: %" SVf, PMCP_WHO, |
| 127 |
|
| SVfARG(name), k, SVfARG(why)); |
| 128 |
|
| } |
| 129 |
343 |
| return c; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| /* A guard spec: one guard, or a list, or 'none'. Each becomes a code ref |
| 133 |
|
| * through Punk's resolver. Returns NULL for 'none' or no guard, +1 AV else. */ |
| 134 |
337 |
| static AV *pmcp_resolve_guards(pTHX_ SV *app, const char *kind, SV *name, |
| 135 |
|
| SV *spec) |
| 136 |
|
| { |
| 137 |
|
| AV *out; |
| 138 |
|
| SV **items; |
| 139 |
|
| SSize_t i, n; |
| 140 |
|
| SV *one[1]; |
| 141 |
|
| |
| 142 |
337 |
▶ | if (!spec) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!spec) return NULL; |
299 |
38 |
337 |
|
| 143 |
43 |
▶ | if (!SvROK(spec) && SvOK(spec) && strEQ(SvPV_nolen(spec), "none")) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvROK(spec) && SvOK(spec) && strEQ(SvPV_nolen(spec), "none")) |
5 |
33 |
38 |
| if (!SvROK(spec) && SvOK(spec) && strEQ(SvPV_nolen(spec), "none")) |
5 |
0 |
5 |
| if (!SvROK(spec) && SvOK(spec) && strEQ(SvPV_nolen(spec), "none")) |
2 |
3 |
5 |
|
| 144 |
2 |
| return NULL; |
| 145 |
36 |
| pmcp_check_guards(aTHX_ "guard", spec); |
| 146 |
35 |
▶ | if (pmcp_is_array(spec)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_array(spec)) { |
2 |
33 |
35 |
|
| 147 |
2 |
| items = AvARRAY((AV *)SvRV(spec)); |
| 148 |
2 |
| n = av_len((AV *)SvRV(spec)) + 1; |
| 149 |
|
| } |
| 150 |
|
| else { |
| 151 |
33 |
| one[0] = spec; |
| 152 |
33 |
| items = one; |
| 153 |
33 |
| n = 1; |
| 154 |
|
| } |
| 155 |
35 |
| out = newAV(); |
| 156 |
35 |
| sv_2mortal((SV *)out); |
| 157 |
72 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
37 |
35 |
72 |
|
| 158 |
37 |
| SV *g = items[i]; |
| 159 |
37 |
▶ | if (pmcp_is_code(g)) av_push(out, newSVsv(g)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_code(g)) av_push(out, newSVsv(g)); |
33 |
4 |
37 |
|
| 160 |
|
| else { |
| 161 |
4 |
| SV *argv[2], *err = NULL, *code; |
| 162 |
4 |
| argv[0] = g; |
| 163 |
4 |
| argv[1] = sv_2mortal(newSVpvf("the guard of mcp %s '%" SVf "'", |
| 164 |
|
| kind, SVfARG(name))); |
| 165 |
4 |
| code = pmcp_call_method(aTHX_ app, "_resolve_target", argv, 2, |
| 166 |
|
| G_SCALAR, &err); |
| 167 |
4 |
▶ | if (!code) pmcp_croak_sv(aTHX_ sv_2mortal(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!code) pmcp_croak_sv(aTHX_ sv_2mortal(err)); |
0 |
4 |
4 |
|
| 168 |
4 |
| av_push(out, code); |
| 169 |
|
| } |
| 170 |
|
| } |
| 171 |
35 |
| return (AV *)SvREFCNT_inc_simple_NN((SV *)out); |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| /* The annotations, with the hints as JSON booleans. +1 HV ref, or NULL. */ |
| 175 |
331 |
| static SV *pmcp_tool_annotations(pTHX_ SV *name, SV *a) |
| 176 |
|
| { |
| 177 |
|
| HV *in, *out; |
| 178 |
|
| HE *he; |
| 179 |
331 |
▶ | if (!a) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) return NULL; |
200 |
131 |
331 |
|
| 180 |
131 |
▶ | if (!pmcp_is_hash(a)) pmcp_tool_croak(aTHX_ name, "annotations must be a hash"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_hash(a)) pmcp_tool_croak(aTHX_ name, "annotations must be a hash"); |
0 |
131 |
131 |
|
| 181 |
131 |
| in = (HV *)SvRV(a); |
| 182 |
131 |
| pmcp_check_keys(aTHX_ "tool annotation", in, PMCP_ANNOTATION_KEYS); |
| 183 |
130 |
| out = newHV(); |
| 184 |
130 |
| hv_iterinit(in); |
| 185 |
641 |
▶ | while ((he = hv_iternext(in))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(in))) { |
512 |
129 |
641 |
|
| 186 |
|
| STRLEN kl; |
| 187 |
512 |
▶ | const char *k = HePV(he, kl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, kl); |
0 |
512 |
512 |
|
| 188 |
512 |
| SV *v = HeVAL(he); |
| 189 |
512 |
▶ | if (kl == 5 && memEQ(k, "title", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 5 && memEQ(k, "title", 5)) { |
2 |
510 |
512 |
| if (kl == 5 && memEQ(k, "title", 5)) { |
2 |
0 |
2 |
|
| 190 |
2 |
▶ | if (!pmcp_is_text(v)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_text(v)) { |
1 |
1 |
2 |
|
| 191 |
1 |
| SvREFCNT_dec((SV *)out); |
| 192 |
1 |
| pmcp_tool_croak(aTHX_ name, "annotation title must be a string"); |
| 193 |
|
| } |
| 194 |
1 |
| (void)hv_store(out, k, (I32)kl, pmcp_str(aTHX_ v), 0); |
| 195 |
|
| } |
| 196 |
510 |
| else (void)hv_store(out, k, (I32)kl, pmcp_bool(aTHX_ SvTRUE(v)), 0); |
| 197 |
|
| } |
| 198 |
129 |
| return newRV_noinc((SV *)out); |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
|
| /* One tool's list entry for version v. +1 bytes. */ |
| 202 |
987 |
| static SV *pmcp_tool_entry(pTHX_ SV *name, HV *spec, SV *annotations, |
| 203 |
|
| enum pmcp_ver v) |
| 204 |
|
| { |
| 205 |
987 |
| HV *e = newHV(); |
| 206 |
987 |
| SV *rv, *bytes, *err = NULL, *x; |
| 207 |
|
| |
| 208 |
987 |
| (void)hv_stores(e, "name", newSVsv(name)); |
| 209 |
987 |
▶ | if ((x = pmcp_hget(aTHX_ spec, "description"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((x = pmcp_hget(aTHX_ spec, "description"))) |
108 |
879 |
987 |
|
| 210 |
108 |
| (void)hv_stores(e, "description", pmcp_str(aTHX_ x)); |
| 211 |
987 |
| x = pmcp_hget(aTHX_ spec, "input"); |
| 212 |
987 |
▶ | (void)hv_stores(e, "inputSchema", |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(e, "inputSchema", |
402 |
585 |
987 |
|
| 213 |
|
| x ? newSVsv(x) : newRV_noinc((SV *)newHV())); |
| 214 |
987 |
▶ | if (!x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { |
585 |
402 |
987 |
|
| 215 |
585 |
| SV **slot = hv_fetchs(e, "inputSchema", 0); |
| 216 |
585 |
| (void)hv_stores((HV *)SvRV(*slot), "type", newSVpvs("object")); |
| 217 |
|
| } |
| 218 |
987 |
▶ | if (annotations) (void)hv_stores(e, "annotations", newSVsv(annotations)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (annotations) (void)hv_stores(e, "annotations", newSVsv(annotations)); |
387 |
600 |
987 |
|
| 219 |
987 |
▶ | if (v >= PMCP_V_2025_06_18) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v >= PMCP_V_2025_06_18) { |
658 |
329 |
987 |
|
| 220 |
658 |
▶ | if ((x = pmcp_hget(aTHX_ spec, "title"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((x = pmcp_hget(aTHX_ spec, "title"))) |
30 |
628 |
658 |
|
| 221 |
30 |
| (void)hv_stores(e, "title", pmcp_str(aTHX_ x)); |
| 222 |
658 |
▶ | if ((x = pmcp_hget(aTHX_ spec, "output"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((x = pmcp_hget(aTHX_ spec, "output"))) |
22 |
636 |
658 |
|
| 223 |
22 |
| (void)hv_stores(e, "outputSchema", newSVsv(x)); |
| 224 |
|
| } |
| 225 |
987 |
▶ | if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) |
329 |
658 |
987 |
| if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) |
1 |
328 |
329 |
|
| 226 |
1 |
| (void)hv_stores(e, "icons", newSVsv(x)); |
| 227 |
|
| |
| 228 |
987 |
| rv = sv_2mortal(newRV_noinc((SV *)e)); |
| 229 |
987 |
| bytes = pmcp_json_trapped(aTHX_ rv, &err); |
| 230 |
987 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
987 |
987 |
|
| 231 |
0 |
| SV *why = sv_2mortal(err); |
| 232 |
0 |
| croak("%s: mcp tool '%" SVf "' cannot be listed, its description is " |
| 233 |
|
| "not JSON: %" SVf, PMCP_WHO, SVfARG(name), SVfARG(why)); |
| 234 |
|
| } |
| 235 |
987 |
| return bytes; |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
|
| /* ---- freezing --------------------------------------------------------------- */ |
| 239 |
|
| |
| 240 |
101 |
| static void pmcp_tools_free(pTHX_ pmcp_tools *ts) |
| 241 |
|
| { |
| 242 |
|
| IV i; |
| 243 |
|
| int v; |
| 244 |
101 |
▶ | if (!ts) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ts) return; |
0 |
101 |
101 |
|
| 245 |
430 |
▶ | for (i = 0; i < ts->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ts->n; i++) { |
329 |
101 |
430 |
|
| 246 |
329 |
| pmcp_tool *t = &ts->t[i]; |
| 247 |
329 |
▶ | if (t->name) SvREFCNT_dec(t->name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->name) SvREFCNT_dec(t->name); |
329 |
0 |
329 |
|
| 248 |
329 |
▶ | if (t->target) SvREFCNT_dec(t->target); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->target) SvREFCNT_dec(t->target); |
203 |
126 |
329 |
|
| 249 |
329 |
▶ | if (t->input) SvREFCNT_dec(t->input); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->input) SvREFCNT_dec(t->input); |
329 |
0 |
329 |
|
| 250 |
329 |
▶ | if (t->output) SvREFCNT_dec(t->output); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->output) SvREFCNT_dec(t->output); |
11 |
318 |
329 |
|
| 251 |
329 |
▶ | if (t->guards) SvREFCNT_dec((SV *)t->guards); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->guards) SvREFCNT_dec((SV *)t->guards); |
7 |
322 |
329 |
|
| 252 |
329 |
▶ | if (t->oa_method) SvREFCNT_dec(t->oa_method); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->oa_method) SvREFCNT_dec(t->oa_method); |
126 |
203 |
329 |
|
| 253 |
329 |
▶ | if (t->oa_path) SvREFCNT_dec(t->oa_path); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->oa_path) SvREFCNT_dec(t->oa_path); |
126 |
203 |
329 |
|
| 254 |
329 |
▶ | if (t->oa_where) SvREFCNT_dec((SV *)t->oa_where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->oa_where) SvREFCNT_dec((SV *)t->oa_where); |
126 |
203 |
329 |
|
| 255 |
1645 |
▶ | for (v = 0; v <= PMCP_V_LATEST; v++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (v = 0; v <= PMCP_V_LATEST; v++) |
1316 |
329 |
1645 |
|
| 256 |
1316 |
▶ | if (t->entry[v]) SvREFCNT_dec(t->entry[v]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->entry[v]) SvREFCNT_dec(t->entry[v]); |
987 |
329 |
1316 |
|
| 257 |
|
| } |
| 258 |
505 |
▶ | for (v = 0; v <= PMCP_V_LATEST; v++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (v = 0; v <= PMCP_V_LATEST; v++) |
404 |
101 |
505 |
|
| 259 |
404 |
▶ | if (ts->list[v]) Safefree(ts->list[v]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts->list[v]) Safefree(ts->list[v]); |
294 |
110 |
404 |
|
| 260 |
101 |
| Safefree(ts->t); |
| 261 |
101 |
| Safefree(ts); |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
462 |
| static int pmcp_tool_cmp(const void *a, const void *b) |
| 265 |
|
| { |
| 266 |
|
| dTHX; |
| 267 |
462 |
| return sv_cmp(((const pmcp_tool *)a)->name, ((const pmcp_tool *)b)->name); |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
|
| /* Check every tool declaration and freeze the table. `decls` holds the |
| 271 |
|
| * resolved [kind, key, spec, target] records. */ |
| 272 |
98 |
| static pmcp_tools *pmcp_tools_freeze(pTHX_ SV *app, AV *decls, HV *opts) |
| 273 |
|
| { |
| 274 |
|
| pmcp_tools *ts; |
| 275 |
98 |
| SSize_t i, n = av_len(decls) + 1; |
| 276 |
|
| SV *v; |
| 277 |
98 |
▶ | NV dflt_timeout = (v = pmcp_hget(aTHX_ opts, "timeout")) ? SvNV(v) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| NV dflt_timeout = (v = pmcp_hget(aTHX_ opts, "timeout")) ? SvNV(v) : 0; |
0 |
98 |
98 |
|
| 278 |
|
| |
| 279 |
98 |
| Newxz(ts, 1, pmcp_tools); |
| 280 |
98 |
▶ | Newxz(ts->t, n > 0 ? n : 1, pmcp_tool); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(ts->t, n > 0 ? n : 1, pmcp_tool); |
98 |
0 |
98 |
| Newxz(ts->t, n > 0 ? n : 1, pmcp_tool); |
0 |
98 |
98 |
|
| 281 |
98 |
▶ | ts->page_size = (v = pmcp_hget(aTHX_ opts, "page_size")) ? SvIV(v) : 50; |
Branch: 1 branch
| Branch | True | False | Total |
| ts->page_size = (v = pmcp_hget(aTHX_ opts, "page_size")) ? SvIV(v) : 50; |
98 |
0 |
98 |
|
| 282 |
|
| |
| 283 |
|
| /* A croak part way leaks what was built so far. It is a boot croak: the |
| 284 |
|
| * application does not start, so there is nothing to leak into. */ |
| 285 |
365 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
280 |
85 |
365 |
|
| 286 |
280 |
| AV *d = (AV *)SvRV(*av_fetch(decls, i, 0)); |
| 287 |
280 |
| SV *kind = *av_fetch(d, PMCP_D_KIND, 0); |
| 288 |
280 |
| SV *name = *av_fetch(d, PMCP_D_KEY, 0); |
| 289 |
280 |
| HV *spec = (HV *)SvRV(*av_fetch(d, PMCP_D_SPEC, 0)); |
| 290 |
|
| pmcp_tool *t; |
| 291 |
|
| STRLEN nl; |
| 292 |
|
| const char *np; |
| 293 |
|
| SV *annotations; |
| 294 |
|
| int ver; |
| 295 |
|
| |
| 296 |
280 |
▶ | if (!strEQ(SvPV_nolen(kind), "tool")) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!strEQ(SvPV_nolen(kind), "tool")) continue; |
64 |
216 |
280 |
|
| 297 |
216 |
| np = SvPV_const(name, nl); |
| 298 |
216 |
▶ | if (!pmcp_tool_name_ok(np, nl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_tool_name_ok(np, nl)) |
2 |
214 |
216 |
|
| 299 |
2 |
| croak("%s: mcp tool '%" SVf "' has a name that is not 1 to %d of " |
| 300 |
|
| "A-Z a-z 0-9 _ . -", PMCP_WHO, SVfARG(name), PMCP_TOOL_NAME_MAX); |
| 301 |
|
| { |
| 302 |
214 |
| SV *what = sv_2mortal(newSVpvf("mcp tool '%" SVf "' key", SVfARG(name))); |
| 303 |
214 |
| pmcp_check_keys(aTHX_ SvPV_nolen(what), spec, PMCP_TOOL_KEYS); |
| 304 |
|
| } |
| 305 |
213 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "title")) && !pmcp_is_text(v)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "title")) && !pmcp_is_text(v)) |
3 |
210 |
213 |
| if ((v = pmcp_hget(aTHX_ spec, "title")) && !pmcp_is_text(v)) |
1 |
2 |
3 |
|
| 306 |
1 |
| pmcp_tool_croak(aTHX_ name, "title must be a string"); |
| 307 |
212 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "description")) && !pmcp_is_text(v)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "description")) && !pmcp_is_text(v)) |
6 |
206 |
212 |
| if ((v = pmcp_hget(aTHX_ spec, "description")) && !pmcp_is_text(v)) |
0 |
6 |
6 |
|
| 308 |
0 |
| pmcp_tool_croak(aTHX_ name, "description must be a string"); |
| 309 |
212 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "input"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "input"))) |
11 |
201 |
212 |
|
| 310 |
11 |
| pmcp_check_object_schema(aTHX_ name, v, "input"); |
| 311 |
210 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "output"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "output"))) |
12 |
198 |
210 |
|
| 312 |
12 |
| pmcp_check_object_schema(aTHX_ name, v, "output"); |
| 313 |
209 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "icons"))) pmcp_check_icons(aTHX_ v); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "icons"))) pmcp_check_icons(aTHX_ v); |
2 |
207 |
209 |
|
| 314 |
208 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "timeout")) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "timeout")) |
2 |
206 |
208 |
|
| 315 |
2 |
▶ | && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
Branch: 3 branches
| Branch | True | False | Total |
| && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
2 |
0 |
2 |
| && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
2 |
0 |
2 |
| && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) |
1 |
1 |
2 |
|
| 316 |
1 |
| pmcp_tool_croak(aTHX_ name, "timeout must be a number of seconds above 0"); |
| 317 |
|
| |
| 318 |
207 |
| t = &ts->t[ts->n]; |
| 319 |
207 |
| t->name = newSVsv(name); |
| 320 |
207 |
| ts->n++; |
| 321 |
207 |
| t->target = newSVsv(*av_fetch(d, PMCP_D_TARGET, 0)); |
| 322 |
207 |
| v = pmcp_hget(aTHX_ spec, "input"); |
| 323 |
|
| { |
| 324 |
207 |
| SV *dflt = NULL; |
| 325 |
207 |
▶ | if (!v) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) { |
198 |
9 |
207 |
|
| 326 |
198 |
| HV *h = newHV(); |
| 327 |
198 |
| (void)hv_stores(h, "type", newSVpvs("object")); |
| 328 |
198 |
| dflt = sv_2mortal(newRV_noinc((SV *)h)); |
| 329 |
|
| } |
| 330 |
207 |
▶ | t->input = pmcp_compile_schema(aTHX_ name, v ? v : dflt, "input"); |
Branch: 1 branch
| Branch | True | False | Total |
| t->input = pmcp_compile_schema(aTHX_ name, v ? v : dflt, "input"); |
9 |
198 |
207 |
|
| 331 |
|
| } |
| 332 |
206 |
▶ | if ((v = pmcp_hget(aTHX_ spec, "output"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = pmcp_hget(aTHX_ spec, "output"))) |
11 |
195 |
206 |
|
| 333 |
11 |
| t->output = pmcp_compile_schema(aTHX_ name, v, "output"); |
| 334 |
216 |
▶ | t->guards = pmcp_resolve_guards(aTHX_ app, "tool", name, |
Branch: 1 branch
| Branch | True | False | Total |
| t->guards = pmcp_resolve_guards(aTHX_ app, "tool", name, |
10 |
196 |
206 |
|
| 335 |
206 |
| hv_exists(spec, "guard", 5) |
| 336 |
10 |
| ? *hv_fetchs(spec, "guard", 0) : NULL); |
| 337 |
205 |
▶ | if (t->guards) ts->any_guard = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->guards) ts->any_guard = 1; |
7 |
198 |
205 |
|
| 338 |
205 |
▶ | t->timeout = (v = pmcp_hget(aTHX_ spec, "timeout")) ? SvNV(v) : dflt_timeout; |
Branch: 1 branch
| Branch | True | False | Total |
| t->timeout = (v = pmcp_hget(aTHX_ spec, "timeout")) ? SvNV(v) : dflt_timeout; |
1 |
204 |
205 |
|
| 339 |
205 |
▶ | t->stream = (v = pmcp_hget(aTHX_ spec, "stream")) && SvTRUE(v); |
Branch: 2 branches
| Branch | True | False | Total |
| t->stream = (v = pmcp_hget(aTHX_ spec, "stream")) && SvTRUE(v); |
57 |
148 |
205 |
| t->stream = (v = pmcp_hget(aTHX_ spec, "stream")) && SvTRUE(v); |
57 |
0 |
57 |
|
| 340 |
|
| |
| 341 |
205 |
| annotations = pmcp_tool_annotations(aTHX_ name, |
| 342 |
|
| pmcp_hget(aTHX_ spec, "annotations")); |
| 343 |
203 |
▶ | if (annotations) sv_2mortal(annotations); |
Branch: 1 branch
| Branch | True | False | Total |
| if (annotations) sv_2mortal(annotations); |
3 |
200 |
203 |
|
| 344 |
812 |
▶ | for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) |
609 |
203 |
812 |
|
| 345 |
609 |
| t->entry[ver] = pmcp_tool_entry(aTHX_ name, spec, annotations, |
| 346 |
|
| (enum pmcp_ver)ver); |
| 347 |
|
| } |
| 348 |
85 |
▶ | if (ts->n > 1) qsort(ts->t, (size_t)ts->n, sizeof(pmcp_tool), pmcp_tool_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts->n > 1) qsort(ts->t, (size_t)ts->n, sizeof(pmcp_tool), pmcp_tool_cmp); |
39 |
46 |
85 |
|
| 349 |
|
| { |
| 350 |
|
| int ver; |
| 351 |
340 |
▶ | for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) { |
255 |
85 |
340 |
|
| 352 |
255 |
▶ | Newxz(ts->list[ver], ts->n > 0 ? ts->n : 1, SV *); |
Branch: 2 branches
| Branch | True | False | Total |
| Newxz(ts->list[ver], ts->n > 0 ? ts->n : 1, SV *); |
255 |
0 |
255 |
| Newxz(ts->list[ver], ts->n > 0 ? ts->n : 1, SV *); |
0 |
255 |
255 |
|
| 353 |
864 |
▶ | for (i = 0; i < ts->n; i++) ts->list[ver][i] = ts->t[i].entry[ver]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ts->n; i++) ts->list[ver][i] = ts->t[i].entry[ver]; |
609 |
255 |
864 |
|
| 354 |
|
| } |
| 355 |
|
| } |
| 356 |
85 |
| return ts; |
| 357 |
|
| } |
| 358 |
|
| |
| 359 |
|
| /* By name, or NULL. The table is sorted, so a binary search. */ |
| 360 |
171 |
| static pmcp_tool *pmcp_tool_find(pTHX_ pmcp_tools *ts, SV *name) |
| 361 |
|
| { |
| 362 |
171 |
▶ | IV lo = 0, hi = ts ? ts->n : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| IV lo = 0, hi = ts ? ts->n : 0; |
171 |
0 |
171 |
|
| 363 |
360 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
356 |
4 |
360 |
|
| 364 |
356 |
| IV mid = lo + (hi - lo) / 2; |
| 365 |
356 |
| int c = sv_cmp(name, ts->t[mid].name); |
| 366 |
356 |
▶ | if (c == 0) return &ts->t[mid]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 0) return &ts->t[mid]; |
167 |
189 |
356 |
|
| 367 |
189 |
▶ | if (c < 0) hi = mid; else lo = mid + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0) hi = mid; else lo = mid + 1; |
125 |
64 |
189 |
|
| 368 |
|
| } |
| 369 |
4 |
| return NULL; |
| 370 |
|
| } |
| 371 |
|
| |
| 372 |
|
| #endif /* PMCP_DECL_H */ |