Autopsy 4.23.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AutopsyMcpModule.java
Go to the documentation of this file.
1/*
2 * Autopsy
3 *
4 * Copyright 2026 Sleuth Kit Labs
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.mcp;
20
21import java.beans.PropertyChangeEvent;
22import java.util.EnumSet;
23import java.util.logging.Level;
24import java.util.logging.Logger;
25import org.openide.modules.OnStart;
26import org.sleuthkit.autopsy.casemodule.Case;
27
38@OnStart
39public class AutopsyMcpModule implements Runnable {
40
41 private static final Logger logger = Logger.getLogger(AutopsyMcpModule.class.getName());
42
43 private static AutopsyMcpModule instance;
44
45 private volatile McpServer mcpServer;
46 private volatile boolean caseListenerRegistered = false;
47
52 public static AutopsyMcpModule getInstance() {
53 return instance;
54 }
55
56 @Override
57 public void run() {
58 instance = this;
59 if (McpOptionsPanel.isMcpEnabled()) {
60 try {
62 } catch (Exception ex) {
63 logger.log(Level.SEVERE, "Failed to start Autopsy MCP server", ex);
64 }
65 }
66 }
67
74 public synchronized void enableServer() throws Exception {
75 if (mcpServer == null) {
77 }
78 }
79
84 public synchronized void disableServer() {
85 McpServer server = mcpServer;
86 mcpServer = null;
87 if (server != null) {
88 server.stop();
89 }
90 }
91
96 private synchronized void startServer() throws Exception {
97 McpServer server = new McpServer();
98 server.start(); // throws on port-bind failure or token-file error
99 mcpServer = server;
100
101 // Register the case listener once; it guards against a null server internally.
103 Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), this::onCaseEvent);
105 }
106
107 // Seed with any case already open (e.g. auto-reopen on launch, or enabled mid-session).
108 try {
109 mcpServer.updateCase(Case.getCurrentCase());
110 } catch (IllegalStateException ex) {
111 // No case open — normal state, nothing to seed.
112 }
113 }
114
115 private void onCaseEvent(PropertyChangeEvent evt) {
116 McpServer server = mcpServer;
117 if (server == null) {
118 return;
119 }
120 if (evt.getNewValue() != null) {
121 // Case opened — wire up the query service.
122 server.updateCase((Case) evt.getNewValue());
123 } else {
124 // Case closed — clear the query service; server keeps listening.
125 server.clearCase();
126 }
127 }
128}
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition Case.java:711
synchronized static Logger getLogger(String name)
Definition Logger.java:124
void updateCase(Case openedCase)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.