55import static us .fatehi .utility .Utility .trimToEmpty ;
66
77import java .nio .file .Path ;
8- import java .util .ArrayList ;
9- import java .util .List ;
108import java .util .logging .Level ;
119import schemacrawler .schemacrawler .InfoLevel ;
10+ import schemacrawler .schemacrawler .LimitOptions ;
11+ import schemacrawler .schemacrawler .LimitOptionsBuilder ;
12+ import schemacrawler .schemacrawler .LoadOptions ;
13+ import schemacrawler .schemacrawler .LoadOptionsBuilder ;
14+ import schemacrawler .schemacrawler .SchemaCrawlerOptions ;
15+ import schemacrawler .schemacrawler .SchemaCrawlerOptionsBuilder ;
1216import schemacrawler .tools .command .mcpserver .McpServerTransportType ;
1317import schemacrawler .tools .databaseconnector .EnvironmentalDatabaseConnectionSourceBuilder ;
14- import us .fatehi .utility .datasource .DatabaseConnectionSourceBuilder ;
18+ import schemacrawler .tools .offline .jdbc .OfflineConnectionUtility ;
19+ import us .fatehi .utility .LoggingConfig ;
20+ import us .fatehi .utility .datasource .DatabaseConnectionSource ;
1521import us .fatehi .utility .ioresource .EnvironmentVariableAccessor ;
1622
1723/** Inner class that handles the MCP server setup. */
1824public final class McpServerContext {
1925
2026 private final EnvironmentVariableAccessor envAccessor ;
27+ private final McpServerTransportType transport ;
28+ private final SchemaCrawlerOptions schemaCrawlerOptions ;
2129
2230 /** Default constructor that uses System.getenv */
2331 public McpServerContext () {
@@ -31,60 +39,70 @@ public McpServerContext() {
3139 */
3240 protected McpServerContext (final EnvironmentVariableAccessor envAccessor ) {
3341 this .envAccessor = requireNonNull (envAccessor , "No environment accessor provided" );
42+
43+ final Level logLevel = readLogLevel ();
44+ new LoggingConfig (logLevel );
45+
46+ transport = readTransport ();
47+ schemaCrawlerOptions = buildSchemaCrawlerOptions ();
3448 }
3549
36- /**
37- * Builds the complete argument list from environment variables.
38- *
39- * @return List of command line arguments
40- */
41- public String [] buildArguments () {
50+ public SchemaCrawlerOptions getSchemaCrawlerOptions () {
51+ return schemaCrawlerOptions ;
52+ }
4253
43- final List <String > arguments = new ArrayList <>();
54+ public McpServerTransportType mcpTransport () {
55+ return transport ;
56+ }
4457
45- final List <String > offlineDatabaseArgs = buildOfflineDatabaseArguments ();
46- if (offlineDatabaseArgs .size () > 0 ) {
47- arguments .addAll (offlineDatabaseArgs );
48- } else {
49- final DatabaseConnectionSourceBuilder databaseConnectionSourceBuilder =
50- EnvironmentalDatabaseConnectionSourceBuilder .builder (envAccessor );
51- final List <String > connectionArguments = databaseConnectionSourceBuilder .toArguments ();
58+ protected DatabaseConnectionSource buildCatalogDatabaseConnectionSource () {
5259
53- arguments .addAll (connectionArguments );
60+ final String offlineDatabasePathString =
61+ trimToEmpty (envAccessor .getenv ("SCHCRWLR_OFFLINE_DATABASE" ));
62+ if (isBlank (offlineDatabasePathString )) {
63+ return buildOperationsDatabaseConnectionSource ();
5464 }
5565
56- addSchemaCrawlerArguments (arguments );
66+ final Path offlineDatabasePath = Path .of (offlineDatabasePathString );
67+ final DatabaseConnectionSource dbConnectionSource =
68+ OfflineConnectionUtility .newOfflineDatabaseConnectionSource (offlineDatabasePath );
69+ return dbConnectionSource ;
70+ }
71+
72+ /**
73+ * Builds the complete argument list from environment variables.
74+ *
75+ * @return List of command line arguments
76+ */
77+ protected DatabaseConnectionSource buildOperationsDatabaseConnectionSource () {
5778
58- return arguments .toArray (new String [0 ]);
79+ final DatabaseConnectionSource databaseConnectionSource =
80+ EnvironmentalDatabaseConnectionSourceBuilder .builder (envAccessor ).build ();
81+ return databaseConnectionSource ;
5982 }
6083
6184 /**
6285 * Adds SchemaCrawler specific arguments to the arguments list.
6386 *
6487 * @param arguments The list of arguments to add to
6588 */
66- protected void addSchemaCrawlerArguments (final List <String > arguments ) {
67- final String infoLevel = envAccessor .getenv ("SCHCRWLR_INFO_LEVEL" );
68- arguments .add ("--info-level" );
69- arguments .add (validInfoLevel (infoLevel ).name ());
70-
71- final String logLevel = envAccessor .getenv ("SCHCRWLR_LOG_LEVEL" );
72- arguments .add ("--log-level" );
73- arguments .add (validLogLevel (logLevel ).getName ());
74-
75- arguments .add ("--routines" );
76- arguments .add (".*" );
77- arguments .add ("--sequences" );
78- arguments .add (".*" );
79- arguments .add ("--synonyms" );
80- arguments .add (".*" );
81-
82- arguments .add ("--command" );
83- arguments .add ("mcpserver" );
84-
85- final String transport = envAccessor .getenv ("SCHCRWLR_MCP_SERVER_TRANSPORT" );
86- arguments .add ("--transport" );
87- arguments .add (validTransport (transport ).name ());
89+ protected SchemaCrawlerOptions buildSchemaCrawlerOptions () {
90+ final InfoLevel infoLevel = readInfoLevel ();
91+
92+ final LoadOptions loadOptions = LoadOptionsBuilder .builder ().withInfoLevel (infoLevel ).build ();
93+ final LimitOptions limitOptions =
94+ LimitOptionsBuilder .builder ()
95+ .includeAllRoutines ()
96+ .includeAllSequences ()
97+ .includeAllSynonyms ()
98+ .includeAllTables ()
99+ .build ();
100+
101+ final SchemaCrawlerOptions schemaCrawlerOptions =
102+ SchemaCrawlerOptionsBuilder .newSchemaCrawlerOptions ()
103+ .withLoadOptions (loadOptions )
104+ .withLimitOptions (limitOptions );
105+ return schemaCrawlerOptions ;
88106 }
89107
90108 /**
@@ -93,13 +111,11 @@ protected void addSchemaCrawlerArguments(final List<String> arguments) {
93111 * @param value The info level string to check
94112 * @return InfoLevel Non-null value
95113 */
96- protected InfoLevel validInfoLevel (final String value ) {
97- final InfoLevel defaultValue = InfoLevel .standard ;
114+ protected InfoLevel readInfoLevel () {
98115
99- if (isBlank (value )) {
100- return defaultValue ;
101- }
116+ final InfoLevel defaultValue = InfoLevel .standard ;
102117 try {
118+ final String value = envAccessor .getenv ("SCHCRWLR_INFO_LEVEL" );
103119 InfoLevel infoLevel = InfoLevel .valueOfFromString (value );
104120 if (infoLevel == InfoLevel .unknown ) {
105121 infoLevel = defaultValue ;
@@ -116,9 +132,11 @@ protected InfoLevel validInfoLevel(final String value) {
116132 * @param value The log level string to check
117133 * @return Level Non-null value
118134 */
119- protected Level validLogLevel (final String value ) {
135+ protected Level readLogLevel () {
136+
120137 final Level defaultValue = Level .INFO ;
121138
139+ final String value = envAccessor .getenv ("SCHCRWLR_LOG_LEVEL" );
122140 if (isBlank (value )) {
123141 return defaultValue ;
124142 }
@@ -135,9 +153,12 @@ protected Level validLogLevel(final String value) {
135153 * @param value The transport string to check
136154 * @return McpServerTransportType Non-null value
137155 */
138- protected McpServerTransportType validTransport (final String value ) {
156+ protected McpServerTransportType readTransport () {
157+ requireNonNull (envAccessor , "No environmental accessor provided" );
158+
139159 final McpServerTransportType defaultValue = McpServerTransportType .stdio ;
140160
161+ final String value = envAccessor .getenv ("SCHCRWLR_MCP_SERVER_TRANSPORT" );
141162 if (isBlank (value )) {
142163 return defaultValue ;
143164 }
@@ -151,27 +172,4 @@ protected McpServerTransportType validTransport(final String value) {
151172 return defaultValue ;
152173 }
153174 }
154-
155- private List <String > buildOfflineDatabaseArguments () {
156-
157- final List <String > arguments = new ArrayList <>();
158-
159- final String offlineDatabasePathString =
160- trimToEmpty (envAccessor .getenv ("SCHCRWLR_OFFLINE_DATABASE" ));
161- if (isBlank (offlineDatabasePathString )) {
162- return arguments ;
163- }
164-
165- final Path offlineDatabasePath = Path .of (offlineDatabasePathString );
166- if (!offlineDatabasePath .toFile ().exists () && !offlineDatabasePath .toFile ().isFile ()) {
167- return arguments ;
168- }
169-
170- arguments .add ("--server" );
171- arguments .add ("offline" );
172- arguments .add ("--database" );
173- arguments .add (offlineDatabasePath .toAbsolutePath ().toString ());
174-
175- return arguments ;
176- }
177175}
0 commit comments