netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! Kafka protocol actions and LLM integration
//!
//! Rust owns the Kafka wire format; the model owns the content. Every event declared
//! here is emitted by `src/server/kafka/mod.rs`, and every action declared here is
//! consumed there. ApiVersions is the one request the model never sees — it advertises
//! what this code can parse, which is not a content decision.

use crate::llm::actions::protocol_trait::{ActionResult, Protocol};
use crate::llm::actions::{ActionDefinition, Parameter, ParameterDefinition, Server};
use crate::protocol::log_template::LogTemplate;
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{anyhow, Result};
use once_cell::sync::Lazy;
use serde_json::{json, Value};

/// Event: Kafka produce request received.
///
/// Emitted once per (topic, partition) in a Produce request, after Rust has decoded and
/// decompressed the record batch. A batch that cannot be decoded never reaches the
/// model: the producer gets CORRUPT_MESSAGE.
pub static PRODUCE_REQUEST_EVENT: Lazy<EventType> = Lazy::new(|| {
    EventType::new(
        "kafka_produce_request",
        "Triggered when a Kafka producer sends records to a topic partition",
        json!({
            "type": "produce_response",
            "topic": "orders",
            "partition": 0,
            "offset": 42,
            "error_code": 0
        }),
    )
    .with_actions(vec![produce_response_action(), error_response_action()])
    .with_parameters(vec![
        Parameter {
            name: "topic".to_string(),
            type_hint: "string".to_string(),
            description: "Topic name".to_string(),
            required: true,
        },
        Parameter {
            name: "partition".to_string(),
            type_hint: "number".to_string(),
            description: "Partition number".to_string(),
            required: true,
        },
        Parameter {
            name: "record_count".to_string(),
            type_hint: "number".to_string(),
            description: "Number of records in the batch".to_string(),
            required: true,
        },
        Parameter {
            name: "first_key".to_string(),
            type_hint: "string".to_string(),
            description: "Key of the first record, null if it has none".to_string(),
            required: false,
        },
        Parameter {
            name: "first_value_preview".to_string(),
            type_hint: "string".to_string(),
            description: "Value of the first record, truncated for the prompt".to_string(),
            required: true,
        },
        Parameter {
            name: "records".to_string(),
            type_hint: "array".to_string(),
            description: "Up to 20 decoded records: [{offset, timestamp, key, key_encoding, \
                          value, value_encoding}]. `*_encoding` is \"utf8\" when the bytes were \
                          printable text and \"hex\" when they were not"
                .to_string(),
            required: true,
        },
        Parameter {
            name: "acks".to_string(),
            type_hint: "number".to_string(),
            description: "Producer's requested acknowledgement level. With acks=0 the producer \
                          is not waiting for a reply and no response is sent"
                .to_string(),
            required: false,
        },
        Parameter {
            name: "client_id".to_string(),
            type_hint: "string".to_string(),
            description: "client_id from the request header (empty if absent)".to_string(),
            required: false,
        },
        Parameter {
            name: "api_version".to_string(),
            type_hint: "number".to_string(),
            description: "Produce API version the client negotiated".to_string(),
            required: false,
        },
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("Kafka produce {topic}")
            .with_debug(
                "Kafka produce topic={topic}, partition={partition}, records={record_count}",
            )
            .with_trace("Kafka produce: {json_pretty(.)}"),
    )
});

/// Event: Kafka fetch request received.
///
/// Emitted once per (topic, partition) in a Fetch request. The broker keeps no log, so
/// the records in the reply are whatever the model, script or static handler supplies.
pub static FETCH_REQUEST_EVENT: Lazy<EventType> = Lazy::new(|| {
    EventType::new(
        "kafka_fetch_request",
        "Triggered when a Kafka consumer requests records from a topic partition",
        json!({
            "type": "fetch_response",
            "topic": "orders",
            "partition": 0,
            "records": [
                {"offset": 40, "key": "order123", "value": "{\"item\": \"laptop\"}"},
                {"offset": 41, "key": "order124", "value": "{\"item\": \"mouse\"}"}
            ]
        }),
    )
    .with_actions(vec![fetch_response_action(), error_response_action()])
    .with_parameters(vec![
        Parameter {
            name: "topic".to_string(),
            type_hint: "string".to_string(),
            description: "Topic name".to_string(),
            required: true,
        },
        Parameter {
            name: "partition".to_string(),
            type_hint: "number".to_string(),
            description: "Partition number".to_string(),
            required: true,
        },
        Parameter {
            name: "fetch_offset".to_string(),
            type_hint: "number".to_string(),
            description: "Offset the consumer wants to read from. Returned records start here"
                .to_string(),
            required: true,
        },
        Parameter {
            name: "max_bytes".to_string(),
            type_hint: "number".to_string(),
            description: "Maximum bytes the consumer will accept for this partition".to_string(),
            required: true,
        },
        Parameter {
            name: "client_id".to_string(),
            type_hint: "string".to_string(),
            description: "client_id from the request header (empty if absent)".to_string(),
            required: false,
        },
        Parameter {
            name: "api_version".to_string(),
            type_hint: "number".to_string(),
            description: "Fetch API version the client negotiated".to_string(),
            required: false,
        },
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("Kafka fetch {topic}")
            .with_debug("Kafka fetch topic={topic}, partition={partition}, offset={fetch_offset}")
            .with_trace("Kafka fetch: {json_pretty(.)}"),
    )
});

/// Event: Kafka metadata request received.
///
/// Emitted once per Metadata request. This is the request that decides whether a client
/// can do anything at all: it must learn which topics exist and which broker leads each
/// partition.
pub static METADATA_REQUEST_EVENT: Lazy<EventType> = Lazy::new(|| {
    EventType::new(
        "kafka_metadata_request",
        "Triggered when a client requests cluster/topic metadata",
        json!({
            "type": "metadata_response",
            "brokers": [{"id": 0, "host": "localhost", "port": 9092}],
            "topics": [
                {
                    "name": "orders",
                    "partitions": [{"partition": 0, "leader": 0, "replicas": [0]}]
                }
            ]
        }),
    )
    .with_actions(vec![metadata_response_action(), error_response_action()])
    .with_parameters(vec![
        Parameter {
            name: "requested_topics".to_string(),
            type_hint: "array".to_string(),
            description:
                "Topic names the client asked about. Any name here that your response \
                          does not describe is reported to the client as UNKNOWN_TOPIC_OR_PARTITION"
                    .to_string(),
            required: true,
        },
        Parameter {
            name: "all_topics".to_string(),
            type_hint: "boolean".to_string(),
            description: "True when the client asked for every topic rather than named ones"
                .to_string(),
            required: true,
        },
        Parameter {
            name: "client_id".to_string(),
            type_hint: "string".to_string(),
            description: "client_id from the request header (empty if absent)".to_string(),
            required: false,
        },
        Parameter {
            name: "api_version".to_string(),
            type_hint: "number".to_string(),
            description: "Metadata API version the client negotiated".to_string(),
            required: false,
        },
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("Kafka metadata request")
            .with_debug("Kafka metadata request, topics={requested_topics}")
            .with_trace("Kafka metadata: {json_pretty(.)}"),
    )
});

/// Event: Kafka offset commit request received.
///
/// Emitted once per (topic, partition) in an OffsetCommit request. Nothing is stored, and
/// OffsetFetch is not implemented, so an accepted commit is an acknowledgement and
/// nothing more.
pub static OFFSET_COMMIT_REQUEST_EVENT: Lazy<EventType> = Lazy::new(|| {
    EventType::new(
        "kafka_offset_commit_request",
        "Triggered when a consumer commits offsets for a topic partition",
        json!({
            "type": "offset_commit_response",
            "topic": "orders",
            "partition": 0,
            "error_code": 0
        }),
    )
    .with_actions(vec![
        offset_commit_response_action(),
        error_response_action(),
    ])
    .with_parameters(vec![
        Parameter {
            name: "group_id".to_string(),
            type_hint: "string".to_string(),
            description: "Consumer group ID".to_string(),
            required: true,
        },
        Parameter {
            name: "topic".to_string(),
            type_hint: "string".to_string(),
            description: "Topic name".to_string(),
            required: true,
        },
        Parameter {
            name: "partition".to_string(),
            type_hint: "number".to_string(),
            description: "Partition number".to_string(),
            required: true,
        },
        Parameter {
            name: "offset".to_string(),
            type_hint: "number".to_string(),
            description: "Offset the consumer wants to commit".to_string(),
            required: true,
        },
        Parameter {
            name: "client_id".to_string(),
            type_hint: "string".to_string(),
            description: "client_id from the request header (empty if absent)".to_string(),
            required: false,
        },
        Parameter {
            name: "api_version".to_string(),
            type_hint: "number".to_string(),
            description: "OffsetCommit API version the client negotiated".to_string(),
            required: false,
        },
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("Kafka offset commit {topic}")
            .with_debug("Kafka offset commit group={group_id}, topic={topic}, partition={partition}, offset={offset}")
            .with_trace("Kafka offset commit: {json_pretty(.)}"),
    )
});

/// Kafka protocol implementation
pub struct KafkaProtocol;

impl KafkaProtocol {
    pub fn new() -> Self {
        Self
    }
}

impl Default for KafkaProtocol {
    fn default() -> Self {
        Self::new()
    }
}

// Helper functions for action definitions

fn produce_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "produce_response".to_string(),
        description: "Answer a Kafka produce request: accept the records at an offset, or \
                      reject them with an error code"
            .to_string(),
        parameters: vec![
            Parameter {
                name: "topic".to_string(),
                type_hint: "string".to_string(),
                description: "Topic name (echo the topic from the event)".to_string(),
                required: true,
            },
            Parameter {
                name: "partition".to_string(),
                type_hint: "number".to_string(),
                description: "Partition number (echo the partition from the event)".to_string(),
                required: true,
            },
            Parameter {
                name: "offset".to_string(),
                type_hint: "number".to_string(),
                description: "Base offset assigned to the first record in the batch".to_string(),
                required: true,
            },
            Parameter {
                name: "error_code".to_string(),
                type_hint: "number".to_string(),
                description: "Kafka error code; 0 (default) accepts the batch".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "produce_response",
            "topic": "orders",
            "partition": 0,
            "offset": 42,
            "error_code": 0
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Kafka produce OK offset={offset}")
                .with_debug(
                    "Kafka produce_response: topic={topic}, partition={partition}, offset={offset}",
                ),
        ),
    }
}

fn fetch_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "fetch_response".to_string(),
        description: "Answer a Kafka fetch request with the records the consumer should see. \
                      An empty array is a valid answer meaning 'nothing new'"
            .to_string(),
        parameters: vec![
            Parameter {
                name: "topic".to_string(),
                type_hint: "string".to_string(),
                description: "Topic name (echo the topic from the event)".to_string(),
                required: true,
            },
            Parameter {
                name: "partition".to_string(),
                type_hint: "number".to_string(),
                description: "Partition number (echo the partition from the event)".to_string(),
                required: true,
            },
            Parameter {
                name: "records".to_string(),
                type_hint: "array".to_string(),
                description: "Records to return: [{offset, key, value, key_encoding, \
                              value_encoding}]. `key`/`value` are text by default; set \
                              `value_encoding` to \"hex\" to send raw bytes as a hex string. \
                              Offsets are made contiguous starting at the first record's offset, \
                              which is never lower than the request's fetch_offset"
                    .to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "fetch_response",
            "topic": "orders",
            "partition": 0,
            "records": [
                {"offset": 40, "key": "order123", "value": "{\"item\": \"laptop\"}"},
                {"offset": 41, "key": "order124", "value": "{\"item\": \"mouse\"}"}
            ]
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Kafka fetch {records_len} records")
                .with_debug("Kafka fetch_response: topic={topic}, partition={partition}, {records_len} records"),
        ),
    }
}

fn metadata_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "metadata_response".to_string(),
        description: "Answer a Kafka metadata request: which brokers exist and which topics and \
                      partitions they lead"
            .to_string(),
        parameters: vec![
            Parameter {
                name: "brokers".to_string(),
                type_hint: "array".to_string(),
                description: "Brokers [{id, host, port}]. Omit this to advertise this NetGet \
                              server itself, which is almost always what you want — a client \
                              cannot connect to a broker address that does not exist"
                    .to_string(),
                required: false,
            },
            Parameter {
                name: "topics".to_string(),
                type_hint: "array".to_string(),
                description: "Topics [{name, partitions: [{partition, leader, replicas}]}]. \
                              `leader` defaults to this broker's id. A topic listed with no \
                              partitions is given one partition led by this broker. Any topic \
                              the client asked about but you omit is reported as unknown"
                    .to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "metadata_response",
            "brokers": [{"id": 0, "host": "localhost", "port": 9092}],
            "topics": [
                {
                    "name": "orders",
                    "partitions": [{"partition": 0, "leader": 0, "replicas": [0]}]
                }
            ]
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Kafka metadata {topics_len} topics")
                .with_debug("Kafka metadata_response: {brokers_len} brokers, {topics_len} topics"),
        ),
    }
}

fn offset_commit_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "offset_commit_response".to_string(),
        description: "Acknowledge (or reject) an offset commit".to_string(),
        parameters: vec![
            Parameter {
                name: "topic".to_string(),
                type_hint: "string".to_string(),
                description: "Topic name (echo the topic from the event)".to_string(),
                required: true,
            },
            Parameter {
                name: "partition".to_string(),
                type_hint: "number".to_string(),
                description: "Partition number (echo the partition from the event)".to_string(),
                required: true,
            },
            Parameter {
                name: "error_code".to_string(),
                type_hint: "number".to_string(),
                description: "Kafka error code; 0 (default) accepts the commit".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "offset_commit_response",
            "topic": "orders",
            "partition": 0,
            "error_code": 0
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Kafka offset commit OK")
                .with_debug("Kafka offset_commit_response: topic={topic}, partition={partition}"),
        ),
    }
}

fn error_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "error_response".to_string(),
        description: "Refuse the request. The error code is carried in the correct response type \
                      for whichever API was called, so the client sees a proper Kafka error \
                      rather than silence"
            .to_string(),
        parameters: vec![
            Parameter {
                name: "error_code".to_string(),
                type_hint: "number".to_string(),
                description: "Kafka error code, e.g. 3 = UNKNOWN_TOPIC_OR_PARTITION, \
                              -1 = UNKNOWN_SERVER_ERROR. 0 is not accepted here: an error \
                              response that says success is treated as -1"
                    .to_string(),
                required: true,
            },
            Parameter {
                name: "error_message".to_string(),
                type_hint: "string".to_string(),
                description: "Human-readable description, logged only — Kafka's wire format has \
                              no place for it in these responses"
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "error_response",
            "error_code": 3,
            "error_message": "Unknown topic or partition"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Kafka error {error_code}")
                .with_debug("Kafka error_response: code={error_code}, message={error_message}"),
        ),
    }
}

// Implement Protocol trait (common functionality)
impl Protocol for KafkaProtocol {
    fn get_startup_parameters(&self) -> Vec<ParameterDefinition> {
        vec![
            ParameterDefinition {
                name: "cluster_id".to_string(),
                type_hint: "string".to_string(),
                description: "Cluster identifier reported in Metadata responses (v2 and above; \
                              earlier Metadata versions have no such field)"
                    .to_string(),
                required: false,
                example: json!("netget-kafka-1"),
                default: None,
            },
            ParameterDefinition {
                name: "broker_id".to_string(),
                type_hint: "number".to_string(),
                description: "Broker ID reported in Metadata, and the default partition leader"
                    .to_string(),
                required: false,
                example: json!(0),
                default: None,
            },
            ParameterDefinition {
                name: "advertised_host".to_string(),
                type_hint: "string".to_string(),
                description: "Hostname advertised to clients in Metadata when the response does \
                              not name one. Defaults to the bound address, or localhost when \
                              bound to a wildcard address"
                    .to_string(),
                required: false,
                example: json!("localhost"),
                default: None,
            },
        ]
    }

    /// Kafka is strictly pull-based: a broker cannot push a record to a consumer, it can
    /// only answer a Fetch. And this broker stores nothing, so there is no topic list to
    /// create or delete against. There is therefore no useful server-initiated action —
    /// an earlier version declared `publish_message`, `create_topic`, `delete_topic` and
    /// `set_retention`, none of which had, or could have, an implementation.
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        Vec::new()
    }

    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![
            produce_response_action(),
            fetch_response_action(),
            metadata_response_action(),
            offset_commit_response_action(),
            error_response_action(),
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "KAFKA"
    }
    fn get_event_types(&self) -> Vec<EventType> {
        vec![
            PRODUCE_REQUEST_EVENT.clone(),
            FETCH_REQUEST_EVENT.clone(),
            METADATA_REQUEST_EVENT.clone(),
            OFFSET_COMMIT_REQUEST_EVENT.clone(),
        ]
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>KAFKA"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["kafka", "kafka broker", "via kafka"]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
            // Beta since September 2026: librdkafka -- a separate C implementation, not the
            // kafka-protocol crate this server frames with -- completes a produce and a fetch
            // against it, and so do Apache Kafka's Java client and franz-go. Not Stable: the
            // other five conditions have not been worked through, and consumer groups do not
            // work at all (see notes).
            .state(DevelopmentState::Beta)
            .well_known_port(9092)
            .implementation(
                "kafka-protocol v0.14 wire format. Implements ApiVersions v0-3 (answered by Rust), \
                 Metadata v0-12, Produce v0-11, Fetch v0-16, OffsetCommit v0-9: the newest \
                 versions the crate knows, flexible (tagged-field) ones included. Topic ids \
                 (Metadata v10+, Fetch v13+) are derived from the topic name, since nothing is \
                 stored to keep an assigned one",
            )
            .llm_control(
                "Metadata, Produce, Fetch and OffsetCommit responses. The model chooses which \
                 topics and partitions exist, whether a produce is accepted and at what offset, \
                 and exactly which records a fetch returns",
            )
            .e2e_testing(
                "Three independent clients, none built on the kafka-protocol crate this server \
                 frames with. kcat 1.7 on librdkafka (tests/server/kafka/real_client_test.rs): \
                 ApiVersions -> Metadata -> Produce and a second run that Fetches and prints the \
                 key and value back, so librdkafka accepted the batch this server encoded; it \
                 negotiates Metadata v12, Produce v7 and Fetch v11. Apache Kafka's own Java \
                 client 3.9.1 and franz-go 1.18.1 (tests/server/kafka/flexible_versions_test.rs) \
                 each produce and then consume at the newest versions, Metadata v12, Produce \
                 v11 and Fetch v16 by topic id, and print a record whose value names the Fetch \
                 version it was served at. None is #[ignore]d; all fail rather than skip. Also \
                 mocked E2E against kafka-protocol's own client-side codecs, with the tshark \
                 oracle. UNPROVEN: consumer groups (the APIs do not exist here), compression, \
                 transactions, TLS/SASL, multi-broker metadata, any Fetch that must resolve an \
                 offset (ListOffsets is not implemented), and OffsetCommit v8+ from a \
                 third-party client, which would need a coordinator first.",
            )
            .notes(
                "Supports exactly five API keys: ApiVersions (18) v0-3, Metadata (3) v0-12, \
                 Produce (0) v0-11, Fetch (1) v0-16, OffsetCommit (8) v0-9. Topic ids are a \
                 hash of the name (stable across restarts); one this broker never reported gets \
                 UNKNOWN_TOPIC_ID (100), and the most recent 4096 are remembered for resolving \
                 Fetch v13+ and Metadata v12 by-id requests. Any other API key — ListOffsets, \
                 FindCoordinator, JoinGroup, SyncGroup, Heartbeat, OffsetFetch, the admin APIs \
                 — is not advertised and closes the connection with a logged error if sent, so \
                 consumer groups do not work: a consumer must assign partitions manually and \
                 fetch from an explicit offset. The broker stores nothing, so a Fetch returns \
                 only what the model supplies for that request and a committed offset is \
                 acknowledged but not remembered. When the model returns no usable action the \
                 client gets UNKNOWN_SERVER_ERROR (-1) in the correct response type, never a \
                 fabricated success.",
            )
            .max_inbound_bytes(crate::server::kafka::MAX_REQUEST_BYTES)
            .build()
    }
    fn description(&self) -> &'static str {
        "Apache Kafka broker: ApiVersions, Metadata, Produce, Fetch and OffsetCommit, with the \
         LLM deciding topics, offsets and record contents"
    }
    fn example_prompt(&self) -> &'static str {
        "Start a Kafka broker on port 9092 with a topic 'orders' with one partition; accept \
         produces and return the produced records when a consumer fetches"
    }
    fn group_name(&self) -> &'static str {
        "Database"
    }

    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;

        StartupExamples::new(
            // LLM mode: the model answers each request
            json!({
                "type": "open_server",
                "port": 9092,
                "base_stack": "kafka",
                "instruction": "Kafka broker with a single topic 'orders' with one partition. \
                                Accept every produce and assign sequential offsets. Return the \
                                produced records on fetch."
            }),
            // Script mode: deterministic responses, no LLM call
            json!({
                "type": "open_server",
                "port": 9092,
                "base_stack": "kafka",
                "event_handlers": [{
                    "event_pattern": "kafka_metadata_request",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "actions = [{'type': 'metadata_response', 'topics': [{'name': 'orders', 'partitions': [{'partition': 0}]}]}]"
                    }
                }]
            }),
            // Static mode: fixed responses
            json!({
                "type": "open_server",
                "port": 9092,
                "base_stack": "kafka",
                "event_handlers": [{
                    "event_pattern": "kafka_produce_request",
                    "handler": {
                        "type": "static",
                        "actions": [{
                            "type": "produce_response",
                            "topic": "orders",
                            "partition": 0,
                            "offset": 0,
                            "error_code": 0
                        }]
                    }
                }]
            }),
        )
    }
}

// Implement Server trait (server-specific functionality)
impl Server for KafkaProtocol {
    fn spawn(
        &self,
        ctx: crate::protocol::SpawnContext,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<std::net::SocketAddr>> + Send>>
    {
        Box::pin(async move {
            use crate::server::kafka::KafkaServer;
            KafkaServer::spawn_with_llm_actions(
                ctx.legacy_listen_addr(),
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.server_id,
                ctx.startup_params,
            )
            .await
        })
    }
    fn execute_action(&self, action: Value) -> Result<ActionResult> {
        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow!("Missing 'type' field in action"))?;

        match action_type {
            // Every one of these is consumed by src/server/kafka/mod.rs, which turns it
            // into the corresponding Kafka response body.
            "produce_response"
            | "fetch_response"
            | "metadata_response"
            | "offset_commit_response"
            | "error_response" => Ok(ActionResult::Custom {
                name: action_type.to_string(),
                data: action,
            }),
            // The dashboard's "[ disconnect this peer ]" injects a bare
            // `{"type": "close_connection"}` through the peer handle, whatever a protocol
            // calls its own close verb. Deliberately NOT advertised in `get_sync_actions`
            // or `get_async_actions`: Kafka has no close message, so the model has nothing
            // to gain from it, and adding it to the tool list would change what the model
            // sees. The generic peer-command task half-closes the write side; the reader's
            // `UnexpectedEof` path then runs the normal teardown.
            "close_connection" => Ok(ActionResult::CloseConnection),
            _ => Err(anyhow!("Unknown Kafka action type: {}", action_type)),
        }
    }
}