001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.examples.mcp;
018
019import java.util.*;
020
021/**
022 * The example's entire "domain": a tiny in-memory map of note title → note body.
023 *
024 * <p>
025 * Every MCP surface the {@link ExampleMcpServer} exposes (tools, prompt, resources, resource
026 * template, completions) is just a thin wrapper over this one object, so a reader can see the
027 * whole scenario at a glance without wading through business logic. It is deliberately trivial;
028 * the interesting part is the MCP wiring, not the storage.
029 *
030 * <p>
031 * Access is synchronized because subscription/tool handlers can run on different threads
032 * (the held-open SSE pump, concurrent HTTP requests). A {@link LinkedHashMap} keeps listing
033 * order stable so the demo output is deterministic.
034 */
035public final class NoteStore {
036
037   /** URI scheme used to address a single note, e.g. {@code note:///groceries}. */
038   public static final String SCHEME = "note:///";
039
040   private final Map<String,String> notes = new LinkedHashMap<>();
041
042   /**
043    * Stores (or replaces) a note.
044    *
045    * @param title The note title (its key). Must not be <jk>null</jk>.
046    * @param body The note body. Must not be <jk>null</jk>.
047    */
048   public synchronized void put(String title, String body) {
049      notes.put(title, body);
050   }
051
052   /**
053    * Returns the body of a note.
054    *
055    * @param title The note title. Can be <jk>null</jk>.
056    * @return The body, or <jk>null</jk> if no such note exists.
057    */
058   public synchronized String get(String title) {
059      return notes.get(title);
060   }
061
062   /**
063    * Removes a note.
064    *
065    * @param title The note title. Can be <jk>null</jk>.
066    * @return <jk>true</jk> if a note was actually removed.
067    */
068   public synchronized boolean remove(String title) {
069      return notes.remove(title) != null;
070   }
071
072   /**
073    * Returns a snapshot of all note titles, in insertion order.
074    *
075    * @return An immutable, point-in-time copy of the titles. Never <jk>null</jk>.
076    */
077   public synchronized List<String> titles() {
078      return List.copyOf(notes.keySet());
079   }
080
081   /**
082    * Builds the canonical resource URI for a note title (e.g. {@code note:///groceries}).
083    *
084    * @param title The note title.
085    * @return The resource URI.
086    */
087   public static String uriFor(String title) {
088      return SCHEME + title;
089   }
090}