-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdan-gen.py
More file actions
355 lines (309 loc) · 9.4 KB
/
Copy pathdan-gen.py
File metadata and controls
355 lines (309 loc) · 9.4 KB
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
#!/usr/bin/env python3
import argparse
import sys
import os
import certifi
os.environ.setdefault("SSL_CERT_FILE", certifi.where())
os.environ.setdefault("REQUESTS_CA_BUNDLE", certifi.where())
from dan import AIDISCA_RRTYPE, AIINDEX_RRTYPE, AIDISCA_TYPE_NAME, AIINDEX_TYPE_NAME
from dan.aidisca import (
AIDISCARecord,
agent_card_extension,
protocol_to_value,
presentation_format_aidisca,
verbose_summary_aidisca
)
from dan.mcp_adapter import get_mcp_capabilities
from dan.a2a_adapter import extract_a2a_metadata
from dan.dane import parse_dane, load_pem_certificate, certificate_association_data
from dan.dnswire import ensure_fqdn, rfc3597_record_one_line
from dan.aiindex import (
AIINDEXRecord,
presentation_format_aiindex,
verbose_summary_aiindex
)
def parse_comma_list(value: str) -> list[str]:
items = [item.strip() for item in value.split(",")]
items = [item for item in items if ensure_fqdn(item)]
if not items:
raise ValueError("At least one agent name is required")
return items
def _capabilities_string(values: str | list[str]) -> str:
if isinstance(values, str):
parts = values.split(",")
else:
parts = values
clean = []
for part in parts:
value = str(part).strip()
if value and value not in clean:
clean.append(value)
if not clean:
raise ValueError("Capabilities cannot be empty")
return ",".join(clean)
def gen_aidisca(args) -> int:
proto = args.proto.lower()
if proto == "mcp" and not args.endpoint:
print("--endpoint is required for MCP.", file=sys.stderr)
return 2
if proto == "a2a" and not args.card:
print("--card is required for A2A.", file=sys.stderr)
return 2
proto_val = protocol_to_value(args.proto)
cert_usage, selector, matching_type = parse_dane(args.dane)
endpoint = args.endpoint
capabilities_source = "operator override"
extensions = b""
if proto == "a2a":
try:
card_endpoint, card_capabilities = extract_a2a_metadata(args.card)
if not endpoint and card_endpoint:
endpoint = card_endpoint
if args.capabilities:
capabilities = _capabilities_string(args.capabilities)
else:
capabilities = _capabilities_string(card_capabilities)
capabilities_source = "A2A Agent Card"
extensions += agent_card_extension(args.card)
except Exception as exc:
print(f"Could not derive A2A metadata: {exc}", file=sys.stderr)
print(
"For A2A, provide a valid --card, or provide "
"--endpoint and --capabilities manually.",
file=sys.stderr,
)
return 2
elif args.capabilities:
capabilities = _capabilities_string(args.capabilities)
elif proto == "mcp":
try:
result = get_mcp_capabilities(args.endpoint, args.auth_token)
capabilities = _capabilities_string(result.capabilities)
capabilities_source = result.source
except Exception as exc:
print("Could not derive Capabilities from MCP service endpoint.", file=sys.stderr)
print(f"Reason: {exc}", file=sys.stderr)
print('Re-run with --capabilities "tool_one,tool_two".', file=sys.stderr)
return 2
else:
print(
"Capabilities are required unless they can be derived from "
"A2A Agent Card or MCP tools/list.",
file=sys.stderr,
)
return 2
if not endpoint:
if proto == "a2a":
print(
"Could not determine Service Endpoint from A2A Agent Card. "
"Provide --service-endpoint explicitly.",
file=sys.stderr,
)
else:
print("--service-endpoint is required for MCP.", file=sys.stderr)
return 2
cert = load_pem_certificate(args.cert_file)
cert_assoc_data = certificate_association_data(cert, selector, matching_type)
record = AIDISCARecord(
proto=proto_val,
capabilities=capabilities,
endpoint=endpoint,
cert_usage=cert_usage,
selector=selector,
matching_type=matching_type,
cert_assoc_data=cert_assoc_data,
extensions=extensions,
)
rdata = record.encode()
print()
print(
rfc3597_record_one_line(
args.domain,
AIDISCA_RRTYPE,
rdata,
args.ttl,
)
)
if args.pf:
print()
print("Presentation Format")
print("----------------------")
print(
presentation_format_aidisca(
ensure_fqdn(args.domain),
args.ttl,
record,
)
)
if args.verbose:
print()
print("Presentation Format")
print("----------------------")
print(
presentation_format_aidisca(
ensure_fqdn(args.domain),
args.ttl,
record,
)
)
print()
print(
verbose_summary_aidisca(
ensure_fqdn(args.domain),
args.ttl,
record,
args.card,
capabilities_source,
args.cert_file,
args.auth_token,
)
)
print()
print()
return 0
def gen_aiindex(args) -> int:
all_agents = parse_comma_list(args.agents)
record = AIINDEXRecord(
agents=all_agents
)
rdata = record.encode()
print()
print(
rfc3597_record_one_line(
args.domain,
AIINDEX_RRTYPE,
rdata,
args.ttl,
)
)
if args.pf:
print()
print("Presentation Format")
print("----------------------")
print(
presentation_format_aiindex(
ensure_fqdn(args.domain),
args.ttl,
record,
)
)
if args.verbose:
print()
print("Presentation Format")
print("----------------------")
print(
presentation_format_aiindex(
ensure_fqdn(args.domain),
args.ttl,
record,
)
)
print()
print(
verbose_summary_aiindex(
ensure_fqdn(args.domain),
args.ttl,
record,
)
)
print()
print()
return 0
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="DAN generation tool for AIDISCA and AIINDEX records."
)
sub = parser.add_subparsers(dest="command", required=True)
aidisca = sub.add_parser("aidisca", help="Generate an AIDISCA ({AIDISCA_RRTYPE}) record")
aidisca.add_argument(
"--proto",
choices=["mcp", "a2a"],
required=True,
help="AI Agent Proto field",
)
aidisca.add_argument(
"--domain",
required=True,
help="Domain name where the AIDISCA record is published",
)
aidisca.add_argument(
"--endpoint",
help=(
"Service Endpoint field. Required for MCP. "
"Optional override for A2A; otherwise derived from Agent Card."
),
)
aidisca.add_argument(
"--capabilities",
help="Comma-separated Capabilities field; overrides automatic extraction",
)
aidisca.add_argument(
"--cert-file",
required=True,
help="PEM certificate file used to compute Certificate Association Data",
)
aidisca.add_argument(
"--dane",
default="3,1,1",
help="DANE parameters as CertUsage,Selector,MatchingType. Default: 3,1,1",
)
aidisca.add_argument(
"--card",
help="A2A Agent Card URL. Required for A2A. Encoded as an Agent Card extension.",
)
aidisca.add_argument(
"--auth-token",
help="Bearer token for MCP capability discovery; never written to DNS output",
)
aidisca.add_argument(
"--ttl",
type=int,
default=60,
help="DNS TTL. Default: 60",
)
aidisca.add_argument(
"--pf",
action="store_true",
help="Print only AIDISCA presentation format.",
)
aidisca.add_argument(
"--verbose",
action="store_true",
help="Print only verbose summary",
)
aidisca.set_defaults(func=gen_aidisca)
aiindex = sub.add_parser("aiindex", help="Generate an AIINDEX ({AIINDEX_RRTYPE}) record")
aiindex.add_argument(
"--domain",
required=True,
help="Domain name where the AIINDEX record is published",
)
aiindex.add_argument(
"--agents",
required=True,
help="Comma separated AIDISCA publication names.",
)
aiindex.add_argument(
"--ttl",
type=int,
default=60,
help="DNS TTL. Default: 60",
)
aiindex.add_argument(
"--pf",
action="store_true",
help="Print only AIINDEX presentation format.",
)
aiindex.add_argument(
"--verbose",
action="store_true",
help="Print only verbose summary",
)
aiindex.set_defaults(func=gen_aiindex)
return parser
def main() -> int:
parser = build_parser()
args = parser.parse_args()
return args.func(args)
if __name__ == "__main__":
raise SystemExit(main())