mirror of
https://github.com/jlengrand/ghost-mcp.git
synced 2026-03-10 08:21:19 +00:00
🎨 Implemented dynamic module discovery and import
This commit is contained in:
@@ -1,91 +1,56 @@
|
|||||||
"""Ghost MCP tools package."""
|
"""Ghost MCP tools package.
|
||||||
|
|
||||||
# Re-export all tools from their respective modules
|
This module dynamically imports all tools from the tools package directory.
|
||||||
from .tools.posts import (
|
It automatically discovers and imports all non-private functions and variables
|
||||||
search_posts_by_title,
|
from Python files in the tools directory, making them available at the package level.
|
||||||
list_posts,
|
|
||||||
read_post,
|
|
||||||
create_post,
|
|
||||||
update_post,
|
|
||||||
delete_post,
|
|
||||||
batchly_update_post
|
|
||||||
)
|
|
||||||
from .tools.users import (
|
|
||||||
list_users,
|
|
||||||
read_user,
|
|
||||||
update_user,
|
|
||||||
delete_user
|
|
||||||
)
|
|
||||||
from .tools.members import (
|
|
||||||
list_members,
|
|
||||||
read_member,
|
|
||||||
create_member,
|
|
||||||
update_member
|
|
||||||
)
|
|
||||||
from .tools.tiers import (
|
|
||||||
list_tiers,
|
|
||||||
read_tier,
|
|
||||||
create_tier,
|
|
||||||
update_tier
|
|
||||||
)
|
|
||||||
from .tools.offers import (
|
|
||||||
list_offers,
|
|
||||||
read_offer,
|
|
||||||
create_offer,
|
|
||||||
update_offer
|
|
||||||
)
|
|
||||||
from .tools.newsletters import (
|
|
||||||
list_newsletters,
|
|
||||||
read_newsletter,
|
|
||||||
create_newsletter,
|
|
||||||
update_newsletter
|
|
||||||
)
|
|
||||||
from .tools.roles import list_roles
|
|
||||||
from .tools.invites import create_invite
|
|
||||||
from .tools.webhooks import create_webhook, update_webhook, delete_webhook
|
|
||||||
from .tools.tags import (
|
|
||||||
browse_tags,
|
|
||||||
read_tag,
|
|
||||||
create_tag,
|
|
||||||
update_tag,
|
|
||||||
delete_tag
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
When adding new tools:
|
||||||
'search_posts_by_title',
|
1. Create new Python files in the tools directory
|
||||||
'list_posts',
|
2. Define your tools as functions in these files
|
||||||
'read_post',
|
3. No need to modify this file - tools will be imported automatically
|
||||||
'create_post',
|
"""
|
||||||
'update_post',
|
|
||||||
'delete_post',
|
from importlib import import_module
|
||||||
'batchly_update_post',
|
from pathlib import Path
|
||||||
'list_users',
|
from typing import Dict, Any
|
||||||
'read_user',
|
|
||||||
'update_user',
|
def _import_submodules() -> Dict[str, Any]:
|
||||||
'delete_user',
|
"""Dynamically import all modules from the tools package.
|
||||||
'list_members',
|
|
||||||
'read_member',
|
Returns:
|
||||||
'create_member',
|
Dict mapping module names to imported module objects
|
||||||
'list_tiers',
|
|
||||||
'read_tier',
|
Raises:
|
||||||
'create_tier',
|
FileNotFoundError: If the tools directory doesn't exist
|
||||||
'update_tier',
|
"""
|
||||||
'list_offers',
|
current_dir = Path(__file__).parent
|
||||||
'read_offer',
|
tools_dir = current_dir / 'tools'
|
||||||
'create_offer',
|
|
||||||
'update_offer',
|
if not tools_dir.exists():
|
||||||
'list_newsletters',
|
raise FileNotFoundError(f"Tools directory not found at: {tools_dir}")
|
||||||
'read_newsletter',
|
|
||||||
'create_newsletter',
|
modules: Dict[str, Any] = {}
|
||||||
'update_newsletter',
|
for py_file in tools_dir.glob('*.py'):
|
||||||
'list_roles',
|
if py_file.name.startswith('__'):
|
||||||
'create_invite',
|
continue
|
||||||
'create_webhook',
|
|
||||||
'update_webhook',
|
module_name = py_file.stem
|
||||||
'delete_webhook',
|
full_module_name = f"ghost_mcp.tools.{module_name}"
|
||||||
'browse_tags',
|
|
||||||
'read_tag',
|
# Import the module
|
||||||
'create_tag',
|
module = import_module(f".tools.{module_name}", package="ghost_mcp")
|
||||||
'update_tag',
|
modules[module_name] = module
|
||||||
'delete_tag'
|
|
||||||
]
|
# Get all non-private attributes
|
||||||
|
for attr_name in dir(module):
|
||||||
|
if not attr_name.startswith('_'):
|
||||||
|
# Add to the current module's namespace
|
||||||
|
globals()[attr_name] = getattr(module, attr_name)
|
||||||
|
|
||||||
|
return modules
|
||||||
|
|
||||||
|
# Run the dynamic imports
|
||||||
|
_import_submodules()
|
||||||
|
|
||||||
|
# Create sorted __all__ from the imported attributes for consistent ordering
|
||||||
|
__all__ = sorted(name for name in globals() if not name.startswith('_'))
|
||||||
|
|||||||
Reference in New Issue
Block a user