forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseCreateMcp.ts
More file actions
163 lines (137 loc) · 4.55 KB
/
Copy pathuseCreateMcp.ts
File metadata and controls
163 lines (137 loc) · 4.55 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
import {
agentPrimitive,
gatewayPrimitive,
gatewayTargetPrimitive,
policyEnginePrimitive,
} from '../../primitives/registry';
import type { AddGatewayConfig } from '../screens/mcp/types';
import { useCallback, useEffect, useState } from 'react';
interface CreateGatewayResult {
name: string;
}
interface CreateStatus<T> {
state: 'idle' | 'loading' | 'success' | 'error';
error?: string;
result?: T;
}
export function useCreateGateway() {
const [status, setStatus] = useState<CreateStatus<CreateGatewayResult>>({ state: 'idle' });
const createGateway = useCallback(async (config: AddGatewayConfig) => {
setStatus({ state: 'loading' });
try {
const addResult = await gatewayPrimitive.add({
name: config.name,
description: config.description,
authorizerType: config.authorizerType,
discoveryUrl: config.jwtConfig?.discoveryUrl,
allowedAudience: config.jwtConfig?.allowedAudience?.join(','),
allowedClients: config.jwtConfig?.allowedClients?.join(','),
allowedScopes: config.jwtConfig?.allowedScopes?.join(','),
customClaims: config.jwtConfig?.customClaims,
clientId: config.jwtConfig?.clientId,
clientSecret: config.jwtConfig?.clientSecret,
enableSemanticSearch: config.enableSemanticSearch,
exceptionLevel: config.exceptionLevel,
policyEngine: config.policyEngineConfiguration?.policyEngineName,
policyEngineMode: config.policyEngineConfiguration?.mode,
});
if (!addResult.success) {
throw new Error(addResult.error ?? 'Failed to create gateway');
}
const result: CreateGatewayResult = { name: config.name };
setStatus({ state: 'success', result });
return { ok: true as const, result };
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to create gateway.';
setStatus({ state: 'error', error: message });
return { ok: false as const, error: message };
}
}, []);
const reset = useCallback(() => {
setStatus({ state: 'idle' });
}, []);
return { status, createGateway, reset };
}
export function useExistingGateways() {
const [gateways, setGateways] = useState<string[]>([]);
useEffect(() => {
async function load() {
const result = await gatewayPrimitive.getExistingGateways();
setGateways(result);
}
void load();
}, []);
const refresh = useCallback(async () => {
const result = await gatewayPrimitive.getExistingGateways();
setGateways(result);
}, []);
return { gateways, refresh };
}
export function useExistingPolicyEngines() {
const [engines, setEngines] = useState<string[]>([]);
useEffect(() => {
async function load() {
const result = await policyEnginePrimitive.getExistingEngines();
setEngines(result);
}
void load();
}, []);
const refresh = useCallback(async () => {
const result = await policyEnginePrimitive.getExistingEngines();
setEngines(result);
}, []);
return { engines, refresh };
}
export function useAvailableAgents() {
const [agents, setAgents] = useState<string[] | null>(null);
useEffect(() => {
async function load() {
try {
const removable = await agentPrimitive.getRemovable();
setAgents(removable.map(a => a.name));
} catch {
setAgents([]);
}
}
void load();
}, []);
const refresh = useCallback(async () => {
try {
const removable = await agentPrimitive.getRemovable();
setAgents(removable.map(a => a.name));
} catch {
setAgents([]);
}
}, []);
return { agents: agents ?? [], isLoading: agents === null, refresh };
}
export function useExistingToolNames() {
const [toolNames, setToolNames] = useState<string[]>([]);
useEffect(() => {
async function load() {
const result = await gatewayTargetPrimitive.getExistingToolNames();
setToolNames(result);
}
void load();
}, []);
const refresh = useCallback(async () => {
const result = await gatewayTargetPrimitive.getExistingToolNames();
setToolNames(result);
}, []);
return { toolNames, refresh };
}
export function useUnassignedTargets() {
const [targets, setTargets] = useState<string[]>([]);
useEffect(() => {
async function load() {
const result = await gatewayPrimitive.getUnassignedTargets();
setTargets(result.map(t => t.name));
}
void load();
}, []);
const refresh = useCallback(async () => {
const result = await gatewayPrimitive.getUnassignedTargets();
setTargets(result.map(t => t.name));
}, []);
return { targets, refresh };
}