001/*
002 * Copyright 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.soklet;
018
019import org.jspecify.annotations.NonNull;
020
021import javax.annotation.concurrent.ThreadSafe;
022
023/**
024 * Thread-safe authentication, authorization, and admission controller for MCP
025 * requests and notifications. Soklet may invoke one controller instance
026 * concurrently for independent messages.
027 *
028 * @author <a href="https://www.revetkn.com">Mark Allen</a>
029 */
030@ThreadSafe
031@FunctionalInterface
032public interface McpAdmissionController {
033        /**
034         * Makes the admission decision for one structurally valid request or
035         * notification.
036         *
037         * @param admissionContext bounded pre-handler admission context
038         * @return a non-null accepted or rejected decision
039         * @throws Exception if application admission logic fails; Soklet fails closed
040         */
041        @NonNull
042        McpAdmissionDecision admit(@NonNull McpAdmissionContext admissionContext) throws Exception;
043
044        /**
045         * Returns the shared controller that accepts every request or notification
046         * as anonymous.
047         *
048         * @return shared accept-all controller
049         */
050        @NonNull
051        static McpAdmissionController acceptAllInstance() {
052                return AcceptAllMcpAdmissionController.INSTANCE;
053        }
054}
055
056/**
057 * Thread-safe accept-all MCP admission controller.
058 *
059 * @author <a href="https://www.revetkn.com">Mark Allen</a>
060 */
061@ThreadSafe
062final class AcceptAllMcpAdmissionController implements McpAdmissionController {
063        @NonNull
064        static final AcceptAllMcpAdmissionController INSTANCE =
065                        new AcceptAllMcpAdmissionController();
066
067        private AcceptAllMcpAdmissionController() {
068        }
069
070        @Override
071        @NonNull
072        public McpAdmissionDecision admit(@NonNull McpAdmissionContext admissionContext) {
073                return McpAdmissionDecision.accepted();
074        }
075}