Go SDK: Align hooks with SEP (#23)
The SEP spec and C# SDK use hooks (plural, JSON array) but the Go SDK
had hook (singular, single object). This meant the wire formats were
incompatible — Go would send `{"hook": {...}}` while everything else
expects `{"hooks": [...]}`.
Signed-off-by: Ukjae Jeong <jeongukjae@gmail.com>
This commit is contained in:
parent
2b1d10093e
commit
23d889254b
2 changed files with 110 additions and 92 deletions
|
|
@ -143,10 +143,7 @@ func (c *Chain) Execute(ctx context.Context, params *ExecutionParams) (*Executio
|
|||
if len(nameFilter) > 0 && !nameFilter[e.Interceptor.Name] {
|
||||
continue
|
||||
}
|
||||
if !matchesPhase(e.Interceptor.Hook.Phase, params.Phase) {
|
||||
continue
|
||||
}
|
||||
if !slices.Contains(e.Interceptor.Hook.Events, params.Event) {
|
||||
if !matchesHooks(e.Interceptor.Hooks, params.Event, params.Phase) {
|
||||
continue
|
||||
}
|
||||
switch e.Interceptor.Type {
|
||||
|
|
@ -432,8 +429,16 @@ func (c *Chain) timeoutResult(cr *ExecutionResult, start time.Time) {
|
|||
})
|
||||
}
|
||||
|
||||
// matchesPhase checks if an interceptor's configured phase covers the target
|
||||
// phase. An interceptor with PhaseBoth matches any target phase.
|
||||
func matchesPhase(interceptorPhase, targetPhase interceptors.InterceptionPhase) bool {
|
||||
return interceptorPhase == interceptors.PhaseBoth || interceptorPhase == targetPhase
|
||||
// matchesHooks checks if any hook entry matches both the target event and phase.
|
||||
// A hook with PhaseBoth matches any target phase.
|
||||
func matchesHooks(hooks []interceptors.Hook, event string, phase interceptors.InterceptionPhase) bool {
|
||||
for _, h := range hooks {
|
||||
if h.Phase != interceptors.PhaseBoth && h.Phase != phase {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(h.Events, event) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,10 +67,12 @@ func registerInterceptorMethods(server *mcp.Server, is []interceptors.Intercepto
|
|||
for _, i := range is {
|
||||
if event != "" {
|
||||
match := false
|
||||
for _, e := range i.GetMetadata().Hook.Events {
|
||||
if e == event {
|
||||
match = true
|
||||
break
|
||||
for _, h := range i.GetMetadata().Hooks {
|
||||
for _, e := range h.Events {
|
||||
if e == event {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
|
|
@ -142,10 +144,10 @@ func TestChain_FailOpenRecordsExecutionResult(t *testing.T) {
|
|||
failOpenValidator := &interceptors.Validator{
|
||||
Metadata: interceptors.Metadata{
|
||||
Name: "fo-validator",
|
||||
Hook: interceptors.Hook{
|
||||
Hooks: []interceptors.Hook{{
|
||||
Events: []string{"test/event"},
|
||||
Phase: interceptors.PhaseRequest,
|
||||
},
|
||||
}},
|
||||
Mode: interceptors.ModeEnforce,
|
||||
FailOpen: true,
|
||||
},
|
||||
|
|
@ -156,10 +158,10 @@ func TestChain_FailOpenRecordsExecutionResult(t *testing.T) {
|
|||
passingValidator := &interceptors.Validator{
|
||||
Metadata: interceptors.Metadata{
|
||||
Name: "passing-validator",
|
||||
Hook: interceptors.Hook{
|
||||
Hooks: []interceptors.Hook{{
|
||||
Events: []string{"test/event"},
|
||||
Phase: interceptors.PhaseRequest,
|
||||
},
|
||||
}},
|
||||
Mode: interceptors.ModeEnforce,
|
||||
},
|
||||
Handler: func(_ context.Context, _ *interceptors.Invocation) (*interceptors.ValidationResult, error) {
|
||||
|
|
@ -190,10 +192,10 @@ func TestChain_FailOpenRecordsExecutionResult(t *testing.T) {
|
|||
failOpenMutator := &interceptors.Mutator{
|
||||
Metadata: interceptors.Metadata{
|
||||
Name: "fo-mutator",
|
||||
Hook: interceptors.Hook{
|
||||
Hooks: []interceptors.Hook{{
|
||||
Events: []string{"test/event"},
|
||||
Phase: interceptors.PhaseResponse,
|
||||
},
|
||||
}},
|
||||
Mode: interceptors.ModeEnforce,
|
||||
FailOpen: true,
|
||||
PriorityHint: interceptors.NewPriority(10),
|
||||
|
|
@ -205,10 +207,10 @@ func TestChain_FailOpenRecordsExecutionResult(t *testing.T) {
|
|||
passingMutator := &interceptors.Mutator{
|
||||
Metadata: interceptors.Metadata{
|
||||
Name: "passing-mutator",
|
||||
Hook: interceptors.Hook{
|
||||
Hooks: []interceptors.Hook{{
|
||||
Events: []string{"test/event"},
|
||||
Phase: interceptors.PhaseResponse,
|
||||
},
|
||||
}},
|
||||
Mode: interceptors.ModeEnforce,
|
||||
PriorityHint: interceptors.NewPriority(20),
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue