include/pmcp/pmcp_state.h

filter: Statementstmt 99.2% 280 / 282 Branchbran 78.5% 157 / 200 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.6% 437 / 482 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_sess.h ↑ Summary include/pmcp/pmcp_decl.h »
1 /* pmcp_state.h - one application's MCP state: the declarations the `mcp`
2 * keyword records, the options `register` validates, and what on_compile
3 * freezes from them.
4 *
5 * Needs: pmcp_reg.h, pmcp_proto.h.
6 *
7 * The state is an HV under the `mcp` key of the application's own hash, as
8 * Punk-Feed keeps its state, so it lives and dies with the application and two
9 * applications in one process cannot share it:
10 *
11 * used how many declarations the keyword has recorded
12 * registered 1 once `plugin 'MCP'` ran
13 * wrapped 1 once the to_app middleware is installed
14 * opts the validated options (pmcp_opts)
15 * decls AV of [kind, key, spec, target] in declaration order, the
16 * target a code ref after on_compile
17 * frozen 1 after on_compile
18 * server the pmcp_srv handle, a Punk::Plugin::MCP::Server object
19 */
20  
21 #ifndef PMCP_STATE_H
22 #define PMCP_STATE_H
23  
24 /* Defined in pmcp_oa.h. */
25 XS_INTERNAL(pmcp_capture_cb);
26  
27 /* Defined in pmcp_sse.h. */
28 XS_INTERNAL(pmcp_helper_progress);
29 XS_INTERNAL(pmcp_helper_log);
30 XS_INTERNAL(pmcp_helper_updated);
31  
32 /* Defined in pmcp_http.h, which needs pmcp_server from this header. */
33 static void pmcp_http_route(pTHX_ SV *app, SV *server, HV *opts);
34 static void pmcp_http_cors_check(pTHX_ SV *inner, HV *opts);
35 static void pmcp_sess_boot_check(pTHX_ SV *app, HV *opts);
36  
37 static const char *const PMCP_KINDS[] = { "tool", "resource", "prompt", NULL };
38  
39 /* The application's MCP state, created on first ask. */
40 1342 static HV *pmcp_state(pTHX_ SV *app)
41 {
42 1342 ▶ HV *h = pmcp_is_hash(app) ? (HV *)SvRV(app) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *h = pmcp_is_hash(app) ? (HV *)SvRV(app) : NULL; 1342 0 1342
43 SV *s;
44 1342 ▶ if (!h) croak("%s: not a Punk application", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("%s: not a Punk application", PMCP_WHO); 0 1342 1342
45 1342 s = pmcp_hget(aTHX_ h, "mcp");
46 1342 ▶ if (pmcp_is_hash(s)) return (HV *)SvRV(s);
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(s)) return (HV *)SvRV(s); 1141 201 1342
47 {
48 201 HV *st = newHV();
49 201 (void)hv_stores(st, "decls", newRV_noinc((SV *)newAV()));
50 201 (void)hv_stores(h, "mcp", newRV_noinc((SV *)st));
51 201 return st;
52 }
53 }
54  
55 783 static AV *pmcp_decls(pTHX_ HV *st)
56 {
57 783 return (AV *)SvRV(pmcp_hget(aTHX_ st, "decls"));
58 }
59  
60 1837 static IV pmcp_flag(pTHX_ HV *st, const char *k)
61 {
62 1837 SV *v = pmcp_hget(aTHX_ st, k);
63 1837 ▶ return v ? SvIV(v) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return v ? SvIV(v) : 0; 779 1058 1837
64 }
65  
66 /* ---- the server handle ------------------------------------------------------ */
67  
68 /* A pmcp_srv held by a blessed pointer object, so it is freed when the
69 * application is. The SVs it points at are owned by the options HV, which the
70 * handle keeps a reference to. */
71 typedef struct {
72 pmcp_srv srv;
73 HV *opts;
74 SV *guards; /* the routes' guard list, an AV ref (11), for stdio */
75 SV *principal; /* `principal` resolved, or NULL */
76 } pmcp_server;
77  
78 /* Every server compiled in this process, for `punk doctor` (13). */
79 static AV *PMCP_SERVERS = NULL;
80  
81 #define PMCP_SERVER_CLASS "Punk::Plugin::MCP::Server"
82  
83 139 static SV *pmcp_server_new(pTHX_ HV *opts, pmcp_tools *ts, int tools,
84 int resources, int prompts)
85 {
86 pmcp_server *ps;
87 SV *obj;
88 SV *v;
89  
90 139 Newxz(ps, 1, pmcp_server);
91 139 ps->opts = (HV *)SvREFCNT_inc_simple_NN((SV *)opts);
92 139 ps->srv.name = pmcp_hget(aTHX_ opts, "name");
93 139 ps->srv.title = pmcp_hget(aTHX_ opts, "title");
94 139 ps->srv.version = pmcp_hget(aTHX_ opts, "version");
95 139 ps->srv.description = pmcp_hget(aTHX_ opts, "description");
96 139 ps->srv.website = pmcp_hget(aTHX_ opts, "website");
97 139 ps->srv.icons = pmcp_hget(aTHX_ opts, "icons");
98 139 ps->srv.instructions = pmcp_hget(aTHX_ opts, "instructions");
99 139 ps->srv.has_tools = tools ? 1 : 0;
100 139 ps->srv.has_resources = resources ? 1 : 0;
101 139 ps->srv.has_prompts = prompts ? 1 : 0;
102 139 ▶ ps->srv.logging = (v = pmcp_hget(aTHX_ opts, "logging")) && SvTRUE(v);
Branch: 2 branches
BranchTrueFalseTotal
ps->srv.logging = (v = pmcp_hget(aTHX_ opts, "logging")) && SvTRUE(v); 139 0 139
ps->srv.logging = (v = pmcp_hget(aTHX_ opts, "logging")) && SvTRUE(v); 24 115 139
103 /* With sessions the GET stream (10) can carry a notification. stdio sets
104 * its own copy (13). */
105 139 ▶ ps->srv.can_notify = (v = pmcp_hget(aTHX_ opts, "session")) && SvTRUE(v);
Branch: 2 branches
BranchTrueFalseTotal
ps->srv.can_notify = (v = pmcp_hget(aTHX_ opts, "session")) && SvTRUE(v); 139 0 139
ps->srv.can_notify = (v = pmcp_hget(aTHX_ opts, "session")) && SvTRUE(v); 15 124 139
106 139 ps->srv.tools = ts;
107 139 ps->srv.log_level = PMCP_LOG_DEFAULT;
108  
109 139 obj = newSV(0);
110 139 sv_setref_pv(obj, PMCP_SERVER_CLASS, (void *)ps);
111 139 return obj;
112 }
113  
114 985 static pmcp_server *pmcp_server_of(pTHX_ SV *obj)
115 {
116 985 ▶ if (!(obj && SvROK(obj) && sv_derived_from(obj, PMCP_SERVER_CLASS)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(obj && SvROK(obj) && sv_derived_from(obj, PMCP_SERVER_CLASS))) 981 4 985
if (!(obj && SvROK(obj) && sv_derived_from(obj, PMCP_SERVER_CLASS))) 981 0 981
if (!(obj && SvROK(obj) && sv_derived_from(obj, PMCP_SERVER_CLASS))) 0 981 981
117 4 return NULL;
118 981 return INT2PTR(pmcp_server *, SvIV(SvRV(obj)));
119 }
120  
121 139 static void pmcp_server_free(pTHX_ pmcp_server *ps)
122 {
123 139 ▶ if (!ps) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) return; 0 139 139
124 139 ▶ if (ps->srv.tools) pmcp_tools_free(aTHX_ (pmcp_tools *)ps->srv.tools);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->srv.tools) pmcp_tools_free(aTHX_ (pmcp_tools *)ps->srv.tools); 101 38 139
125 139 ▶ if (ps->srv.resources)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->srv.resources) 34 105 139
126 34 pmcp_resources_free(aTHX_ (pmcp_resources *)ps->srv.resources);
127 139 ▶ if (ps->srv.prompts) pmcp_prompts_free(aTHX_ (pmcp_prompts *)ps->srv.prompts);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->srv.prompts) pmcp_prompts_free(aTHX_ (pmcp_prompts *)ps->srv.prompts); 9 130 139
128 139 ▶ if (ps->srv.psgi) SvREFCNT_dec(ps->srv.psgi);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->srv.psgi) SvREFCNT_dec(ps->srv.psgi); 19 120 139
129 139 ▶ if (ps->guards) SvREFCNT_dec(ps->guards);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->guards) SvREFCNT_dec(ps->guards); 107 32 139
130 139 ▶ if (ps->principal) SvREFCNT_dec(ps->principal);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->principal) SvREFCNT_dec(ps->principal); 7 132 139
131 139 ▶ if (ps->opts) SvREFCNT_dec((SV *)ps->opts);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->opts) SvREFCNT_dec((SV *)ps->opts); 139 0 139
132 139 Safefree(ps);
133 }
134  
135 /* ---- the `mcp` keyword --------------------------------------------------------- */
136  
137 /* mcp KIND => KEY => {spec} => TARGET
138 * mcp KIND => KEY => TARGET
139 *
140 * Records only. The spec is checked by 05 (tools) and 06 (resources, prompts),
141 * and the target is resolved at to_app, because the controller it names is
142 * often declared below the keyword. cap = [app]. */
143 369 XS_INTERNAL(pmcp_kw_cb)
144 {
145 369 dXSARGS;
146 369 SV *app = pmcp_cap(aTHX_ cv, 0);
147 369 HV *st = pmcp_state(aTHX_ app);
148 369 SV *kind, *key, *spec = NULL, *target;
149 AV *decls, *rec;
150 SSize_t i, n;
151 STRLEN kl;
152 const char *k;
153 int ki;
154  
155 369 ▶ if (items != 3 && items != 4)
Branch: 2 branches
BranchTrueFalseTotal
if (items != 3 && items != 4) 138 231 369
if (items != 3 && items != 4) 1 137 138
156 1 croak("%s: mcp takes KIND => KEY => {spec} => TARGET, as in "
157 "mcp tool => search => {...} => 'Books#search'", PMCP_WHO);
158 368 kind = ST(0);
159 368 key = ST(1);
160 368 ▶ if (items == 4) { spec = ST(2); target = ST(3); }
Branch: 1 branch
BranchTrueFalseTotal
if (items == 4) { spec = ST(2); target = ST(3); } 137 231 368
161 231 else target = ST(2);
162  
163 736 ▶ k = pmcp_is_text(kind) ? SvPV_const(kind, kl) : "";
Branch: 1 branch
BranchTrueFalseTotal
k = pmcp_is_text(kind) ? SvPV_const(kind, kl) : ""; 368 0 368
164 368 ▶ if (!pmcp_is_text(kind)) kl = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(kind)) kl = 0; 0 368 368
165 539 ▶ for (ki = 0; PMCP_KINDS[ki]; ki++)
Branch: 1 branch
BranchTrueFalseTotal
for (ki = 0; PMCP_KINDS[ki]; ki++) 538 1 539
166 538 ▶ if (strlen(PMCP_KINDS[ki]) == kl && memEQ(PMCP_KINDS[ki], k, kl)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(PMCP_KINDS[ki]) == kl && memEQ(PMCP_KINDS[ki], k, kl)) break; 368 170 538
if (strlen(PMCP_KINDS[ki]) == kl && memEQ(PMCP_KINDS[ki], k, kl)) break; 367 1 368
167 368 ▶ if (!PMCP_KINDS[ki])
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_KINDS[ki]) 1 367 368
168 1 croak("%s: mcp takes tool, resource or prompt, not '%.*s'", PMCP_WHO,
169 (int)kl, k);
170 367 ▶ if (!pmcp_is_text(key))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(key)) 2 365 367
171 2 ▶ croak("%s: mcp %s needs a %s", PMCP_WHO, k,
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: mcp %s needs a %s", PMCP_WHO, k, 1 1 2
172 ki == 1 ? "URI" : "name");
173 365 ▶ if (spec && !pmcp_is_hash(spec))
Branch: 2 branches
BranchTrueFalseTotal
if (spec && !pmcp_is_hash(spec)) 137 228 365
if (spec && !pmcp_is_hash(spec)) 1 136 137
174 1 croak("%s: mcp %s '%" SVf "' takes its description as a hash",
175 PMCP_WHO, k, SVfARG(key));
176 364 ▶ if (!pmcp_is_target(aTHX_ target))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_target(aTHX_ target)) 1 363 364
177 1 croak("%s: mcp %s '%" SVf "' needs a code ref or a "
178 "'Controller#method' target", PMCP_WHO, k, SVfARG(key));
179 363 ▶ if (pmcp_flag(aTHX_ st, "frozen"))
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_flag(aTHX_ st, "frozen")) 1 362 363
180 1 croak("%s: mcp %s '%" SVf "' is declared after to_app", PMCP_WHO, k,
181 SVfARG(key));
182  
183 362 decls = pmcp_decls(aTHX_ st);
184 362 n = av_len(decls) + 1;
185 1207 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 846 361 1207
186 846 AV *d = (AV *)SvRV(*av_fetch(decls, i, 0));
187 846 ▶ if (sv_eq(*av_fetch(d, PMCP_D_KIND, 0), kind)
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(*av_fetch(d, PMCP_D_KIND, 0), kind) 639 207 846
188 639 ▶ && sv_eq(*av_fetch(d, PMCP_D_KEY, 0), key))
Branch: 1 branch
BranchTrueFalseTotal
&& sv_eq(*av_fetch(d, PMCP_D_KEY, 0), key)) 1 638 639
189 1 croak("%s: mcp %s '%" SVf "' is declared twice", PMCP_WHO, k,
190 SVfARG(key));
191 }
192  
193 361 rec = newAV();
194 361 av_push(rec, newSVpvn(k, kl));
195 361 av_push(rec, newSVsv(key));
196 361 ▶ av_push(rec, spec ? newSVsv(spec) : newRV_noinc((SV *)newHV()));
Branch: 1 branch
BranchTrueFalseTotal
av_push(rec, spec ? newSVsv(spec) : newRV_noinc((SV *)newHV())); 135 226 361
197 361 av_push(rec, newSVsv(target));
198 361 av_push(decls, newRV_noinc((SV *)rec));
199 361 (void)hv_stores(st, "used", newSViv(pmcp_flag(aTHX_ st, "used") + 1));
200 361 XSRETURN_EMPTY;
201 }
202  
203 /* ---- on_compile: resolve and freeze ----------------------------------------------- */
204  
205 /* Every 'Controller#method' target becomes the code ref it names, through
206 * Punk's own resolver, so a declaration follows the rules a route does. */
207 153 static void pmcp_resolve_targets(pTHX_ SV *app, HV *st)
208 {
209 153 AV *decls = pmcp_decls(aTHX_ st);
210 153 SSize_t i, n = av_len(decls) + 1;
211  
212 509 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 357 152 509
213 357 AV *d = (AV *)SvRV(*av_fetch(decls, i, 0));
214 357 SV *target = *av_fetch(d, PMCP_D_TARGET, 0);
215 357 SV *argv[2], *err = NULL, *code;
216 357 ▶ if (pmcp_is_code(target)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_code(target)) continue; 11 346 357
217 346 argv[0] = target;
218 346 argv[1] = sv_2mortal(newSVpvf("mcp %" SVf " '%" SVf "'",
219 SVfARG(*av_fetch(d, PMCP_D_KIND, 0)),
220 SVfARG(*av_fetch(d, PMCP_D_KEY, 0))));
221 /* _resolve_target croaks by design on a target it cannot find */
222 346 code = pmcp_call_method(aTHX_ app, "_resolve_target", argv, 2,
223 G_SCALAR, &err);
224 346 ▶ if (!code) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!code) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 1 345 346
225 345 ▶ if (!pmcp_is_code(code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_code(code)) { 0 345 345
226 0 SvREFCNT_dec(code);
227 0 croak("%s: %" SVf " did not resolve to code", PMCP_WHO,
228 SVfARG(argv[1]));
229 }
230 345 (void)av_store(d, PMCP_D_TARGET, code);
231 }
232 152 }
233  
234 153 static void pmcp_freeze(pTHX_ SV *app, HV *st)
235 {
236 153 AV *decls = pmcp_decls(aTHX_ st);
237 153 SSize_t i, n = av_len(decls) + 1;
238 153 int counts[3] = { 0, 0, 0 };
239 153 SV *opts = pmcp_hget(aTHX_ st, "opts");
240  
241 153 ▶ if (pmcp_flag(aTHX_ st, "frozen")) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_flag(aTHX_ st, "frozen")) return; 0 153 153
242 153 pmcp_resolve_targets(aTHX_ app, st);
243 508 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 356 152 508
244 356 AV *d = (AV *)SvRV(*av_fetch(decls, i, 0));
245 356 const char *k = SvPV_nolen(*av_fetch(d, PMCP_D_KIND, 0));
246 356 ▶ counts[k[0] == 't' ? 0 : k[0] == 'r' ? 1 : 2]++;
Branch: 2 branches
BranchTrueFalseTotal
counts[k[0] == 't' ? 0 : k[0] == 'r' ? 1 : 2]++; 140 216 356
counts[k[0] == 't' ? 0 : k[0] == 'r' ? 1 : 2]++; 114 26 140
247 }
248 152 ▶ (void)hv_stores(st, "server",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(st, "server", 54 98 152
(void)hv_stores(st, "server", 16 38 54
(void)hv_stores(st, "server", 98 54 152
249 pmcp_server_new(aTHX_ (HV *)SvRV(opts),
250 counts[0] ? pmcp_tools_freeze(aTHX_ app, decls,
251 (HV *)SvRV(opts)) : NULL,
252 counts[0] || pmcp_hget(aTHX_ (HV *)SvRV(opts), "openapi"),
253 counts[1], counts[2]));
254 {
255 139 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server"));
256 139 ▶ if (counts[1])
Branch: 1 branch
BranchTrueFalseTotal
if (counts[1]) 56 83 139
257 34 ps->srv.resources = pmcp_resources_freeze(aTHX_ app, decls,
258 56 (HV *)SvRV(opts));
259 117 ▶ if (counts[2])
Branch: 1 branch
BranchTrueFalseTotal
if (counts[2]) 14 103 117
260 9 ps->srv.prompts = pmcp_prompts_freeze(aTHX_ app, decls,
261 14 (HV *)SvRV(opts));
262 }
263 112 pmcp_http_route(aTHX_ app, pmcp_hget(aTHX_ st, "server"), (HV *)SvRV(opts));
264 107 ▶ if (!PMCP_SERVERS) PMCP_SERVERS = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_SERVERS) PMCP_SERVERS = newAV(); 36 71 107
265 107 av_push(PMCP_SERVERS, newSVsv(pmcp_hget(aTHX_ st, "server")));
266 /* last, so innermost: the raw app, for OpenAPI tools to re-enter (12) */
267 107 ▶ if (pmcp_hget(aTHX_ (HV *)SvRV(opts), "openapi")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_hget(aTHX_ (HV *)SvRV(opts), "openapi")) { 19 88 107
268 19 AV *cap = newAV();
269 19 av_push(cap, newSVsv(app));
270 19 av_push(cap, newSVsv(pmcp_hget(aTHX_ st, "server")));
271 19 pmcp_middleware(aTHX_ app, pmcp_capture_cb, cap);
272 }
273 107 (void)hv_stores(st, "frozen", newSViv(1));
274 }
275  
276 /* cap = [app] */
277 153 XS_INTERNAL(pmcp_compile_cb)
278 {
279 153 dXSARGS;
280 153 SV *app = pmcp_cap(aTHX_ cv, 0);
281 PERL_UNUSED_VAR(items);
282 153 pmcp_freeze(aTHX_ app, pmcp_state(aTHX_ app));
283 107 XSRETURN_EMPTY;
284 }
285  
286 /* ---- to_app: the tripwire and the guard check ------------------------------------------ */
287  
288 /* Code ref address -> "GET /path" for every route that carries a guard: the
289 * router's records, then each OpenAPI mount's operations. Mortal. Read here and
290 * not in on_compile, because the records exist only once the router has
291 * compiled (and Punk::Router::records dereferences NULL before then). */
292 86 static HV *pmcp_guarded_targets(pTHX_ SV *app)
293 {
294 86 HV *out = (HV *)sv_2mortal((SV *)newHV());
295 86 HV *h = (HV *)SvRV(app);
296 86 SV *router = pmcp_hget(aTHX_ h, "router");
297 86 SV *mounts = pmcp_hget(aTHX_ h, "api_mounts");
298 SV *recs;
299 SSize_t i, n;
300  
301 #define PMCP_NOTE_GUARDED(code, where) STMT_START { \
302 char key[32]; \
303 int kl = my_snprintf(key, sizeof key, "%" UVxf, \
304 PTR2UV(SvRV(code))); \
305 if (!hv_exists(out, key, kl)) \
306 (void)hv_store(out, key, kl, (where), 0); \
307 else SvREFCNT_dec(where); \
308 } STMT_END
309  
310 86 ▶ if (router) {
Branch: 1 branch
BranchTrueFalseTotal
if (router) { 86 0 86
311 86 recs = pmcp_call_method(aTHX_ router, "records", NULL, 0, G_SCALAR, NULL);
312 86 ▶ if (recs && pmcp_is_array(recs)) {
Branch: 2 branches
BranchTrueFalseTotal
if (recs && pmcp_is_array(recs)) { 86 0 86
if (recs && pmcp_is_array(recs)) { 86 0 86
313 86 AV *av = (AV *)SvRV(recs);
314 86 n = av_len(av) + 1;
315 237 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 151 86 237
316 151 SV **e = av_fetch(av, i, 0);
317 HV *r;
318 SV *code, *g, *m, *p;
319 151 ▶ if (!(e && pmcp_is_hash(*e))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && pmcp_is_hash(*e))) continue; 151 0 151
if (!(e && pmcp_is_hash(*e))) continue; 0 151 151
320 151 r = (HV *)SvRV(*e);
321 151 code = pmcp_hget(aTHX_ r, "code");
322 151 g = pmcp_hget(aTHX_ r, "guards");
323 302 ▶ if (!(pmcp_is_code(code) && pmcp_is_array(g)
Branch: 1 branch
BranchTrueFalseTotal
if (!(pmcp_is_code(code) && pmcp_is_array(g) 151 0 151
324 280 && av_len((AV *)SvRV(g)) >= 0)) continue;
325 22 m = pmcp_hget(aTHX_ r, "method");
326 22 p = pmcp_hget(aTHX_ r, "path");
327 66 ▶ PMCP_NOTE_GUARDED(code, newSVpvf("%s %s",
Branch: 5 branches
BranchTrueFalseTotal
PMCP_NOTE_GUARDED(code, newSVpvf("%s %s", 22 0 22
PMCP_NOTE_GUARDED(code, newSVpvf("%s %s", 22 0 22
PMCP_NOTE_GUARDED(code, newSVpvf("%s %s", 22 0 22
PMCP_NOTE_GUARDED(code, newSVpvf("%s %s", 0 0 0
PMCP_NOTE_GUARDED(code, newSVpvf("%s %s", 0 0 0
328 m ? SvPV_nolen(m) : "?", p ? SvPV_nolen(p) : "?"));
329 }
330 }
331 86 ▶ if (recs) SvREFCNT_dec(recs);
Branch: 1 branch
BranchTrueFalseTotal
if (recs) SvREFCNT_dec(recs); 86 0 86
332 }
333  
334 86 ▶ if (pmcp_is_array(mounts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_array(mounts)) { 86 0 86
335 86 AV *ms = (AV *)SvRV(mounts);
336 86 n = av_len(ms) + 1;
337 87 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 86 87
338 1 SV **e = av_fetch(ms, i, 0);
339 SV *ops;
340 HE *he;
341 1 ▶ if (!(e && pmcp_is_hash(*e))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && pmcp_is_hash(*e))) continue; 1 0 1
if (!(e && pmcp_is_hash(*e))) continue; 0 1 1
342 1 ops = pmcp_hget(aTHX_ (HV *)SvRV(*e), "ops");
343 1 ▶ if (!pmcp_is_hash(ops)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(ops)) continue; 0 1 1
344 1 hv_iterinit((HV *)SvRV(ops));
345 11 ▶ while ((he = hv_iternext((HV *)SvRV(ops)))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext((HV *)SvRV(ops)))) { 10 1 11
346 10 SV *rec = HeVAL(he), *code, *g;
347 STRLEN ol;
348 const char *op;
349 19 ▶ if (!pmcp_is_hash(rec)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(rec)) continue; 0 10 10
350 10 code = pmcp_hget(aTHX_ (HV *)SvRV(rec), "code");
351 10 g = pmcp_hget(aTHX_ (HV *)SvRV(rec), "guards");
352 20 ▶ if (!(pmcp_is_code(code) && pmcp_is_array(g)
Branch: 1 branch
BranchTrueFalseTotal
if (!(pmcp_is_code(code) && pmcp_is_array(g) 10 0 10
353 19 && av_len((AV *)SvRV(g)) >= 0)) continue;
354 1 ▶ op = HePV(he, ol);
Branch: 1 branch
BranchTrueFalseTotal
op = HePV(he, ol); 0 1 1
355 1 ▶ PMCP_NOTE_GUARDED(code, newSVpvf("operation %.*s", (int)ol, op));
Branch: 1 branch
BranchTrueFalseTotal
PMCP_NOTE_GUARDED(code, newSVpvf("operation %.*s", (int)ol, op)); 1 0 1
356 }
357 }
358 }
359 #undef PMCP_NOTE_GUARDED
360 86 return out;
361 }
362  
363 /* P4: a declaration whose action sits behind a guarded route must say how it
364 * is guarded. A tool calls the action directly, so without this an action
365 * that is admin-only on the web would be callable by anyone past `auth`. */
366 101 static void pmcp_guard_check(pTHX_ SV *app, HV *st)
367 {
368 101 AV *decls = pmcp_decls(aTHX_ st);
369 101 SSize_t i, n = av_len(decls) + 1;
370 101 HV *guarded = NULL;
371  
372 401 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 305 96 401
373 305 AV *d = (AV *)SvRV(*av_fetch(decls, i, 0));
374 305 SV *target = *av_fetch(d, PMCP_D_TARGET, 0);
375 305 SV *spec = *av_fetch(d, PMCP_D_SPEC, 0);
376 SV **where;
377 char key[32];
378 int kl;
379  
380 331 ▶ if (!pmcp_is_code(target)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_code(target)) continue; 0 305 305
381 305 ▶ if (pmcp_is_hash(spec) && hv_exists((HV *)SvRV(spec), "guard", 5))
Branch: 2 branches
BranchTrueFalseTotal
if (pmcp_is_hash(spec) && hv_exists((HV *)SvRV(spec), "guard", 5)) 305 0 305
if (pmcp_is_hash(spec) && hv_exists((HV *)SvRV(spec), "guard", 5)) 26 279 305
382 26 continue;
383 279 ▶ if (!guarded) guarded = pmcp_guarded_targets(aTHX_ app);
Branch: 1 branch
BranchTrueFalseTotal
if (!guarded) guarded = pmcp_guarded_targets(aTHX_ app); 86 193 279
384 279 kl = my_snprintf(key, sizeof key, "%" UVxf, PTR2UV(SvRV(target)));
385 279 where = hv_fetch(guarded, key, kl, 0);
386 279 ▶ if (where)
Branch: 1 branch
BranchTrueFalseTotal
if (where) 5 274 279
387 5 croak("%s: mcp %" SVf " '%" SVf "' runs the action that %" SVf
388 " guards, and a call through MCP would skip that guard. "
389 "Give it `guard => ...`, or `guard => 'none'` to expose it "
390 "without one", PMCP_WHO,
391 SVfARG(*av_fetch(d, PMCP_D_KIND, 0)),
392 SVfARG(*av_fetch(d, PMCP_D_KEY, 0)), SVfARG(*where));
393 }
394 96 }
395  
396 /* The middleware body. Runs once at to_app with the inner app, and returns it
397 * unchanged, so it costs nothing per request. cap = [app]. */
398 105 XS_INTERNAL(pmcp_wrap_cb)
399 {
400 105 dXSARGS;
401 105 SV *app = pmcp_cap(aTHX_ cv, 0);
402 105 HV *st = pmcp_state(aTHX_ app);
403 105 ▶ SV *inner = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *inner = items > 0 ? ST(0) : &PL_sv_undef; 105 0 105
404  
405 105 ▶ if (pmcp_flag(aTHX_ st, "used") && !pmcp_flag(aTHX_ st, "registered"))
Branch: 2 branches
BranchTrueFalseTotal
if (pmcp_flag(aTHX_ st, "used") && !pmcp_flag(aTHX_ st, "registered")) 89 16 105
if (pmcp_flag(aTHX_ st, "used") && !pmcp_flag(aTHX_ st, "registered")) 1 88 89
406 1 croak("%s: mcp was used but plugin 'MCP' was never registered "
407 "(add plugin 'MCP' => {...})", PMCP_WHO);
408 104 ▶ if (pmcp_flag(aTHX_ st, "registered")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_flag(aTHX_ st, "registered")) { 102 2 104
409 102 SV *opts = pmcp_hget(aTHX_ st, "opts");
410 102 ▶ if (!pmcp_flag(aTHX_ st, "used")
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_flag(aTHX_ st, "used") 14 88 102
411 14 ▶ && !(pmcp_is_hash(opts) && pmcp_hget(aTHX_ (HV *)SvRV(opts), "openapi")))
Branch: 2 branches
BranchTrueFalseTotal
&& !(pmcp_is_hash(opts) && pmcp_hget(aTHX_ (HV *)SvRV(opts), "openapi"))) 14 0 14
&& !(pmcp_is_hash(opts) && pmcp_hget(aTHX_ (HV *)SvRV(opts), "openapi"))) 1 13 14
412 1 croak("%s: plugin 'MCP' is registered but nothing is declared "
413 "(use the mcp keyword, or give the plugin an openapi mount)",
414 PMCP_WHO);
415 101 pmcp_guard_check(aTHX_ app, st);
416 96 ▶ if (pmcp_is_hash(opts)) pmcp_http_cors_check(aTHX_ inner, (HV *)SvRV(opts));
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(opts)) pmcp_http_cors_check(aTHX_ inner, (HV *)SvRV(opts)); 96 0 96
417 96 ▶ if (pmcp_is_hash(opts)) pmcp_sess_boot_check(aTHX_ app, (HV *)SvRV(opts));
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(opts)) pmcp_sess_boot_check(aTHX_ app, (HV *)SvRV(opts)); 96 0 96
418 }
419 96 ST(0) = inner;
420 96 XSRETURN(1);
421 }
422  
423 /* ---- installing ----------------------------------------------------------------- */
424  
425 662 static AV *pmcp_cap_app(pTHX_ SV *app)
426 {
427 662 AV *cap = newAV();
428 662 av_push(cap, newSVsv(app));
429 662 return cap;
430 }
431  
432 /* The keyword and the to_app middleware, once each. Both import and register
433 * call this: import so the bareword form parses, register for an application
434 * that names the plugin without `use`-ing it. */
435 329 static void pmcp_install(pTHX_ SV *app)
436 {
437 329 HV *st = pmcp_state(aTHX_ app);
438 329 pmcp_keyword(aTHX_ app, "mcp", pmcp_kw_cb, pmcp_cap_app(aTHX_ app));
439 329 ▶ if (!pmcp_flag(aTHX_ st, "wrapped")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_flag(aTHX_ st, "wrapped")) { 168 161 329
440 168 (void)hv_stores(st, "wrapped", newSViv(1));
441 168 pmcp_middleware(aTHX_ app, pmcp_wrap_cb, pmcp_cap_app(aTHX_ app));
442 }
443 329 }
444  
445 /* register($app, \%opts) */
446 199 static void pmcp_register(pTHX_ SV *app, SV *opts)
447 {
448 199 HV *st = pmcp_state(aTHX_ app);
449 199 SV *class = pmcp_hget(aTHX_ (HV *)SvRV(app), "caller");
450 HV *o;
451  
452 199 ▶ if (pmcp_flag(aTHX_ st, "registered"))
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_flag(aTHX_ st, "registered")) 1 198 199
453 1 ▶ croak("%s: plugin 'MCP' is registered twice on %" SVf, PMCP_WHO,
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: plugin 'MCP' is registered twice on %" SVf, PMCP_WHO, 0 1 1
454 SVfARG(class ? class : sv_2mortal(newSVpvs("this application"))));
455 198 ▶ o = pmcp_opts(aTHX_ class ? class : sv_2mortal(newSVpvs("main")), opts);
Branch: 1 branch
BranchTrueFalseTotal
o = pmcp_opts(aTHX_ class ? class : sv_2mortal(newSVpvs("main")), opts); 0 198 198
456 165 (void)hv_stores(st, "opts", newRV_noinc((SV *)o));
457 165 (void)hv_stores(st, "registered", newSViv(1));
458 165 pmcp_install(aTHX_ app);
459 165 pmcp_helper(aTHX_ app, "mcp", pmcp_helper_mcp);
460 165 pmcp_helper(aTHX_ app, "mcp_user", pmcp_helper_mcp_user);
461 165 pmcp_helper(aTHX_ app, "mcp_progress", pmcp_helper_progress);
462 165 pmcp_helper(aTHX_ app, "mcp_log", pmcp_helper_log);
463 165 pmcp_helper(aTHX_ app, "mcp_resource_updated", pmcp_helper_updated);
464 165 pmcp_at_compile(aTHX_ app, pmcp_compile_cb, pmcp_cap_app(aTHX_ app));
465 165 }
466  
467 /* ---- a view for state_for --------------------------------------------------------- */
468  
469 /* A plain hash of what the application declared and what was frozen, for the
470 * command (13) and the tests. A copy: changing it changes nothing. +1. */
471 14 static SV *pmcp_state_view(pTHX_ SV *app)
472 {
473 14 HV *st = pmcp_state(aTHX_ app);
474 14 HV *out = newHV();
475 14 AV *decls = pmcp_decls(aTHX_ st);
476 14 AV *list = newAV();
477 14 HV *names = newHV();
478 14 SSize_t i, n = av_len(decls) + 1;
479 14 SV *opts = pmcp_hget(aTHX_ st, "opts");
480 int k;
481  
482 56 ▶ for (k = 0; PMCP_KINDS[k]; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; PMCP_KINDS[k]; k++) 42 14 56
483 42 (void)hv_store(names, PMCP_KINDS[k], (I32)strlen(PMCP_KINDS[k]),
484 newRV_noinc((SV *)newAV()), 0);
485 29 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 14 29
486 15 AV *d = (AV *)SvRV(*av_fetch(decls, i, 0));
487 15 HV *e = newHV();
488 15 SV *kind = *av_fetch(d, PMCP_D_KIND, 0);
489 15 HE *slot = hv_fetch_ent(names, kind, 0, 0);
490 15 (void)hv_stores(e, "kind", newSVsv(kind));
491 15 (void)hv_stores(e, "key", newSVsv(*av_fetch(d, PMCP_D_KEY, 0)));
492 15 (void)hv_stores(e, "spec", newSVsv(*av_fetch(d, PMCP_D_SPEC, 0)));
493 15 (void)hv_stores(e, "target", newSVsv(*av_fetch(d, PMCP_D_TARGET, 0)));
494 15 av_push(list, newRV_noinc((SV *)e));
495 15 ▶ if (slot) av_push((AV *)SvRV(HeVAL(slot)),
Branch: 1 branch
BranchTrueFalseTotal
if (slot) av_push((AV *)SvRV(HeVAL(slot)), 15 0 15
496 newSVsv(*av_fetch(d, PMCP_D_KEY, 0)));
497 }
498 14 (void)hv_stores(out, "declarations", newRV_noinc((SV *)list));
499 14 (void)hv_stores(out, "tools", newSVsv(*hv_fetchs(names, "tool", 0)));
500 14 (void)hv_stores(out, "resources", newSVsv(*hv_fetchs(names, "resource", 0)));
501 14 (void)hv_stores(out, "prompts", newSVsv(*hv_fetchs(names, "prompt", 0)));
502 14 SvREFCNT_dec((SV *)names);
503 14 (void)hv_stores(out, "registered", newSViv(pmcp_flag(aTHX_ st, "registered")));
504 14 (void)hv_stores(out, "frozen", newSViv(pmcp_flag(aTHX_ st, "frozen")));
505 14 ▶ if (pmcp_is_hash(opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(opts)) { 13 1 14
506 13 HV *copy = newHVhv((HV *)SvRV(opts));
507 13 (void)hv_stores(out, "opts", newRV_noinc((SV *)copy));
508 }
509 {
510 14 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server"));
511 14 ▶ if (ps) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps) { 10 4 14
512 10 HV *s = newHV();
513 10 (void)hv_stores(s, "tools", newSViv(ps->srv.has_tools));
514 10 (void)hv_stores(s, "resources", newSViv(ps->srv.has_resources));
515 10 (void)hv_stores(s, "prompts", newSViv(ps->srv.has_prompts));
516 10 (void)hv_stores(s, "logging", newSViv(ps->srv.logging));
517 10 (void)hv_stores(s, "can_notify", newSViv(ps->srv.can_notify));
518 10 (void)hv_stores(out, "server", newRV_noinc((SV *)s));
519 }
520 }
521 14 return newRV_noinc((SV *)out);
522 }
523  
524 #endif /* PMCP_STATE_H */
« include/pmcp/pmcp_sess.h ↑ Summary include/pmcp/pmcp_decl.h »