Skip to content

Commit e8e5efb

Browse files
committed
refactor: improve logging messages and server initialization
- Simplify and standardize logging messages across server components - Add more informative server startup messages with connection details - Remove redundant logging while maintaining important information - Clean up initialization logs to be more concise and meaningful
1 parent 482f6db commit e8e5efb

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

src/main/java/com/github/thought2code/mcp/annotated/McpServers.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.github.thought2code.mcp.annotated.server.McpStdioServer;
1111
import com.github.thought2code.mcp.annotated.server.McpStreamableServer;
1212
import com.github.thought2code.mcp.annotated.server.component.ResourceBundleProvider;
13+
import com.github.thought2code.mcp.annotated.util.JacksonHelper;
1314
import io.modelcontextprotocol.server.McpSyncServer;
1415
import io.modelcontextprotocol.util.Assert;
1516
import java.util.Objects;
@@ -76,15 +77,15 @@ private McpServers() {}
7677
*/
7778
public static McpServers run(Class<?> mainClass, String[] args) {
7879
if (servers != null) {
79-
log.warn("McpServers is already initialized");
80+
log.warn("{} is already initialized", mainClass.getSimpleName());
8081
return servers;
8182
}
8283

83-
log.info("Initializing McpServers with main class: {}, args: {}", mainClass.getName(), args);
84+
log.info("Initializing {} with args: {}", mainClass.getSimpleName(), args);
8485
ReflectionsProvider.initializeReflectionsInstance(mainClass);
8586
ResourceBundleProvider.loadResourceBundle(mainClass);
8687
servers = new McpServers();
87-
log.info("McpServers initialized successfully");
88+
log.info("{} initialized successfully", mainClass.getSimpleName());
8889

8990
return servers;
9091
}
@@ -105,10 +106,7 @@ public static McpServers run(Class<?> mainClass, String[] args) {
105106
* @see McpServerConfiguration.Builder
106107
*/
107108
public void startStdioServer(McpServerConfiguration.Builder configuration) {
108-
log.info("Starting McpStdioServer with configuration: {}", configuration);
109-
configuration.enabled(true).mode(ServerMode.STDIO);
110-
doStartServer(configuration.build());
111-
log.info("McpStdioServer started successfully");
109+
doStartServer(configuration.mode(ServerMode.STDIO).build());
112110
}
113111

114112
/**
@@ -127,10 +125,7 @@ public void startStdioServer(McpServerConfiguration.Builder configuration) {
127125
* @see McpServerConfiguration.Builder
128126
*/
129127
public void startSseServer(McpServerConfiguration.Builder configuration) {
130-
log.info("Starting McpSseServer with configuration: {}", configuration);
131-
configuration.enabled(true).mode(ServerMode.SSE);
132-
doStartServer(configuration.build());
133-
log.info("McpSseServer started successfully");
128+
doStartServer(configuration.mode(ServerMode.SSE).build());
134129
}
135130

136131
/**
@@ -149,10 +144,7 @@ public void startSseServer(McpServerConfiguration.Builder configuration) {
149144
* @see McpServerConfiguration.Builder
150145
*/
151146
public void startStreamableServer(McpServerConfiguration.Builder configuration) {
152-
log.info("Starting McpStreamableServer with configuration: {}", configuration);
153-
configuration.enabled(true).mode(ServerMode.STREAMABLE);
154-
doStartServer(configuration.build());
155-
log.info("McpStreamableServer started successfully");
147+
doStartServer(configuration.mode(ServerMode.STREAMABLE).build());
156148
}
157149

158150
/**
@@ -170,10 +162,9 @@ public void startStreamableServer(McpServerConfiguration.Builder configuration)
170162
*/
171163
public void startServer(String configFileName) {
172164
Assert.notNull(configFileName, "configFileName must not be null");
173-
log.info("Starting McpServer with configuration file: {}", configFileName);
165+
log.info("Starting MCP server with configuration file: {}", configFileName);
174166
McpConfigurationLoader configLoader = new McpConfigurationLoader(configFileName);
175167
doStartServer(configLoader.loadConfig());
176-
log.info("McpServer started successfully with configuration file: {}", configFileName);
177168
}
178169

179170
/**
@@ -188,10 +179,9 @@ public void startServer(String configFileName) {
188179
* @see McpServerConfiguration
189180
*/
190181
public void startServer() {
191-
log.info("Starting McpServer with default configuration");
182+
log.info("Starting MCP server with default configuration");
192183
McpConfigurationLoader configLoader = new McpConfigurationLoader();
193184
doStartServer(configLoader.loadConfig());
194-
log.info("McpServer started successfully with default configuration");
195185
}
196186

197187
/**
@@ -218,6 +208,7 @@ public void startServer() {
218208
* @see ServerMode
219209
*/
220210
private void doStartServer(McpServerConfiguration configuration) {
211+
log.info("Starting MCP server with config: {}", JacksonHelper.toJsonString(configuration));
221212
if (configuration.enabled()) {
222213
McpServer mcpServer = null;
223214
switch (configuration.mode()) {

src/main/java/com/github/thought2code/mcp/annotated/reflect/ReflectionsProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void initializeReflectionsInstance(Class<?> mainClass) {
7878
return;
7979
}
8080

81-
log.info("Initializing Reflections instance for main class: {}", mainClass.getName());
81+
log.info("Initializing Reflections instance");
8282
String basePackage = mainClass.getPackageName();
8383
McpServerApplication application = mainClass.getAnnotation(McpServerApplication.class);
8484
if (application != null) {
@@ -90,7 +90,7 @@ public static void initializeReflectionsInstance(Class<?> mainClass) {
9090
}
9191
}
9292
reflections = new Reflections(basePackage, MethodsAnnotated, FieldsAnnotated);
93-
log.info("Reflections instance initialized for base package: {}", basePackage);
93+
log.info("Reflections instance initialized successfully");
9494
}
9595

9696
/**

src/main/java/com/github/thought2code/mcp/annotated/server/JettyHttpServer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ public JettyHttpServer bind(int port) {
6666
/** Start Jetty HTTP server and bind it to the specified port. */
6767
public void start() {
6868
if (server != null && server.isRunning()) {
69-
log.warn("Jetty HTTP server is already started");
69+
log.warn("Jetty-based MCP server is already started");
7070
return;
7171
}
7272

7373
try {
7474
initServer();
7575
server.start();
76-
log.info("Jetty-based MCP server started on port {}", port);
76+
log.info("Jetty-based MCP server started successfully");
7777

7878
final boolean isTesting = Boolean.parseBoolean(System.getProperty("mcp.server.testing"));
7979
if (isTesting) {
@@ -83,7 +83,7 @@ public void start() {
8383

8484
await(server);
8585
} catch (Exception e) {
86-
log.error("Error starting Jetty-based MCP server on port {}", port, e);
86+
log.error("Error starting Jetty-based MCP server", e);
8787
stop();
8888
}
8989
}
@@ -114,7 +114,6 @@ private void initServer() {
114114
*/
115115
private void await(Server server) {
116116
try {
117-
log.info("Jetty-based MCP server is running...");
118117
server.join();
119118
} catch (InterruptedException e) {
120119
log.error("Error joining Jetty-based MCP server", e);
@@ -126,9 +125,9 @@ public void stop() {
126125
if (server != null && server.isRunning()) {
127126
try {
128127
server.stop();
129-
log.info("Jetty-based MCP server stopped on port {}", port);
128+
log.info("Jetty-based MCP server stopped");
130129
} catch (Exception e) {
131-
log.error("Error stopping Jetty-based MCP server on port {}", port, e);
130+
log.error("Error stopping Jetty-based MCP server", e);
132131
} finally {
133132
server.destroy();
134133
server = null;

src/main/java/com/github/thought2code/mcp/annotated/server/McpServerBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void registerComponents(McpSyncServer mcpSyncServer) {
139139
*/
140140
@Override
141141
public McpSyncServer createSyncServer() {
142-
log.info("Creating McpSyncServer with configuration: {}", configuration);
142+
log.info("Creating McpSyncServer with name: {}", configuration.name());
143143
McpSchema.ServerCapabilities serverCapabilities = defineCapabilities();
144144
McpSyncServer mcpSyncServer =
145145
createSyncSpecification()

src/main/java/com/github/thought2code/mcp/annotated/server/McpSseServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.thought2code.mcp.annotated.configuration.McpServerConfiguration;
44
import com.github.thought2code.mcp.annotated.configuration.McpServerSSE;
5+
import com.github.thought2code.mcp.annotated.util.InetHelper;
56
import io.modelcontextprotocol.json.McpJsonMapper;
67
import io.modelcontextprotocol.server.McpServer;
78
import io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider;
@@ -88,6 +89,11 @@ public McpServer.SyncSpecification<?> createSyncSpecification() {
8889
* @see HttpServletSseServerTransportProvider
8990
*/
9091
public void startHttpServer() {
92+
log.info(
93+
"Starting Jetty-based MCP SSE server on http://{}:{}{}",
94+
InetHelper.findFirstNonLoopbackAddress().getHostAddress(),
95+
configuration.sse().port(),
96+
configuration.sse().endpoint());
9197
JettyHttpServer httpServer = new JettyHttpServer();
9298
httpServer.withTransportProvider(transportProvider).bind(port).start();
9399
}

src/main/java/com/github/thought2code/mcp/annotated/server/McpStreamableServer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import com.github.thought2code.mcp.annotated.configuration.McpServerConfiguration;
44
import com.github.thought2code.mcp.annotated.configuration.McpServerStreamable;
5+
import com.github.thought2code.mcp.annotated.util.InetHelper;
56
import io.modelcontextprotocol.json.McpJsonMapper;
67
import io.modelcontextprotocol.server.McpServer;
78
import io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider;
89
import java.time.Duration;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
912

1013
/**
1114
* An MCP server implementation that operates in Streamable HTTP mode.
@@ -40,6 +43,9 @@
4043
* @see JettyHttpServer
4144
*/
4245
public class McpStreamableServer extends McpServerBase {
46+
47+
private static final Logger log = LoggerFactory.getLogger(McpStreamableServer.class);
48+
4349
/** The HTTP Streamable server transport provider used by this MCP server. */
4450
private HttpServletStreamableServerTransportProvider transportProvider;
4551

@@ -114,6 +120,11 @@ public McpServer.SyncSpecification<?> createSyncSpecification() {
114120
* @see #createSyncSpecification()
115121
*/
116122
public void startHttpServer() {
123+
log.info(
124+
"Starting Jetty-based MCP Streamable server on http://{}:{}{}",
125+
InetHelper.findFirstNonLoopbackAddress().getHostAddress(),
126+
configuration.streamable().port(),
127+
configuration.streamable().mcpEndpoint());
117128
JettyHttpServer httpServer = new JettyHttpServer();
118129
httpServer.withTransportProvider(transportProvider).bind(port).start();
119130
}

src/main/java/com/github/thought2code/mcp/annotated/server/component/ResourceBundleProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public static void loadResourceBundle(Class<?> mainClass) {
7171
return;
7272
}
7373

74-
log.info("Loading resource bundle for main class: {}", mainClass.getName());
7574
McpI18nEnabled mcpI18nEnabled = mainClass.getAnnotation(McpI18nEnabled.class);
7675
if (mcpI18nEnabled == null) {
7776
log.info("McpI18nEnabled annotation is not present on the main class, skip i18n support.");
@@ -83,8 +82,9 @@ public static void loadResourceBundle(Class<?> mainClass) {
8382
throw new IllegalArgumentException("resourceBundleBaseName must not be blank.");
8483
}
8584

85+
log.info("Loading resource bundle with base name: {}", baseName);
8686
bundle = Immutable.of(ResourceBundle.getBundle(baseName, Locale.getDefault()));
87-
log.info("Resource bundle loaded for base name: {}", baseName);
87+
log.info("Resource bundle loaded successfully with base name: {}", baseName);
8888
}
8989

9090
/**

0 commit comments

Comments
 (0)